submitted Data
$_GET = [ 'r' => 'site/login',];$_POST = [ '_csrf' => 'WjgxM0tfa3YxQn1rcg4vHioLVlt.EjhOAnFVR3MuXgQ7WkNmBwofIg==', 'LoginForm' => [ 'username' => 'admin', 'password' => 'admin', 'rememberMe' => '1', ], 'login-button' => '',];
YII2 Handling the Administrator login section of the code:
#/var/www/example.com/public_html/yii/backend/controllers/SiteController.php public function actionLogin() { if (!\Yii::$app->user->isGuest) { return $this->goHome(); } $model = new LoginForm(); if ($model->load(Yii::$app->request->post()) && $model->login()) { return $this->goBack(); } else { return $this->render('login', [ 'model' => $model, ]); } }
But I do not have the user table, here is the backend, the Administrator login section, the corresponding table is the admin, structure.
mysql> desc admin;+-------+------------------ +------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+------------------+------+-----+---------+-------+| ID | Int (Ten) unsigned | NO | | NULL | || name | char (30) | NO | | | || PWD | char (32) | NO | | | || email | char (30) | NO | | | |+-------+------------------+------+-----+---------+-------+4 rows in Set (0.03 sec) mysql> select * FROM admin;+---- +---------+----------------------------------+-------+| ID | name | PWD | Email |+----+---------+----------------------------------+-------+| 1 | admin | B9840270f7f4ed699ccc93cc09914d21 | |+----+---------+----------------------------------+-------+1 row in Set (0.00 sec)
Yii2 He defaults to the user table, but I don't have a user table.
I now submit the user name and password, how to modify the code inside the Actionlogin is the most appropriate.
Reply content:
Submitted data
$_GET = [ 'r' => 'site/login',];$_POST = [ '_csrf' => 'WjgxM0tfa3YxQn1rcg4vHioLVlt.EjhOAnFVR3MuXgQ7WkNmBwofIg==', 'LoginForm' => [ 'username' => 'admin', 'password' => 'admin', 'rememberMe' => '1', ], 'login-button' => '',];
YII2 Handling the Administrator login section of the code:
#/var/www/example.com/public_html/yii/backend/controllers/SiteController.php public function actionLogin() { if (!\Yii::$app->user->isGuest) { return $this->goHome(); } $model = new LoginForm(); if ($model->load(Yii::$app->request->post()) && $model->login()) { return $this->goBack(); } else { return $this->render('login', [ 'model' => $model, ]); } }
But I do not have the user table, here is the backend, the Administrator login section, the corresponding table is the admin, structure.
mysql> desc admin;+-------+------------------ +------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+------------------+------+-----+---------+-------+| ID | Int (Ten) unsigned | NO | | NULL | || name | char (30) | NO | | | || PWD | char (32) | NO | | | || email | char (30) | NO | | | |+-------+------------------+------+-----+---------+-------+4 rows in Set (0.03 sec) mysql> select * FROM admin;+---- +---------+----------------------------------+-------+| ID | name | PWD | Email |+----+---------+----------------------------------+-------+| 1 | admin | B9840270f7f4ed699ccc93cc09914d21 | |+----+---------+----------------------------------+-------+1 row in Set (0.00 sec)
Yii2 He defaults to the user table, but I don't have a user table.
I now submit the user name and password, how to modify the code inside the Actionlogin is the most appropriate.
1 refer to the Common\models\user in the advanced template to modify the Admin, and change to the current password encryption method.
2 Modifying the configuration file
'user' => [ // 'identityClass' => 'common\models\User', 'identityClass' => 'common\models\Admin', 'enableAutoLogin' => true, ],
- Modify LoginForm GetUser () to get the user from your user table.
Try 2.0 and models/User.php change it to the bottom.
$token]); The public static function Findbyusername ($username) {//username is modified to name return Static::findone ([' Name ' = $username]); } public Function GetId () {return $this->id; } public Function Getauthkey () {return $this->authkey; The Public Function Validateauthkey ($authKey) {return $this->authkey = = = $authKey; } Public Function ValidatePassword ($password) {//password modified to pwd return $this->pwd = = = $password; }}
You should add a useridentity and modify the LoginForm combination before you can use the user table of the parent class
Very simple, add rights management, fast and convenient. You can see the role management chapter of RBAC in the official documentation. Recommendation: The background administrator and the ordinary user put in a piece. This is the high-level programmer's fan!