且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从 MySQL 中的表登录的 Yii2 分步指南

更新时间:2023-12-03 14:46:22

Yii2 高级应用程序默认带有来自 DB 的登录部分的工作示例(我看到基本应用程序使用静态用户名和密码).您不必安装任何额外的东西,只需查看代码即可.安装高级应用并查看前端.

Yii2 advanced app comes by default with a working example of the login part from the DB (I see the basic ones uses a static username and password). You do not have to install anything extra, just look at the code. Install the advanced app and take a look at the frontend.

简而言之,SiteController 使用 LoginModel 进行验证,然后使用 LoginModel 的 login() 将 User 模型登录到 User 组件.

In short SiteController uses LoginModel for validation then uses the login() of the LoginModel to login the User model to the User component.

如果您不想使用 User 模型,只需创建您自己的模型并使用该模型.您不想使用默认的 User 组件,只需创建您自己的组件即可.这很容易做到.

If you do not want to use the User model, just create your own model and use that one. You do not want to use the default User component, just create your own. It is quite easy to do.

伙计,删除下面变量的公共声明.

mate, remove the public declarations of variables bellow.

class User extends ActiveRecord implements yiiwebIdentityInterface
{
public $id;
public $username;
public $password;
public $authKey;
public $accessToken;

您告诉 Yii 忽略数据库中的内容.

You are telling Yii to ignore what is in the database.