且构网

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

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

更新时间:2023-12-03 15:08:04

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

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组件,只需创建自己的组件即可.这很容易做到.

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 \yii\web\IdentityInterface
{
public $id;
public $username;
public $password;
public $authKey;
public $accessToken;

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

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