Yii2 How to handle admin login

Source: Internet
Author: User
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,    ],
    1. 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!

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.