1. Use the GII to generate a module (modules) named admin
2. Add the following configuration in./config/web.php
' Modules ' = [ ' Admin ' = [ ' Class ' = ' App\modules\admin\module ',//Background Module ] ], ' components ' = [ ' User ' = [ ' Identityclass ' = ' app\models\user ', ' Enableautologin 'true, ' loginurl ' =>['/site/login ',//define the background default login interface [skip to this page for insufficient permissions]' Identitycookie ' = [' name ' = ' __user_identity ', ' httponly ' =true], ' idparam ' = ' __user ' ],//Front and rear login separation' Admin ' = [ ' Class ' = ' Yii\web\user ', ' identityclass ' = ' app\modules\admin\models\adminuser ', ' Enableaut Ologin ' =true, ' loginurl ' =>['/admin/site/login ',//define the background default login interface [skip to this page for insufficient permissions]' Identitycookie ' = [' name ' = ' __admin_identity ', ' httponly ' =true], ' idparam ' = ' __admin ' ],]
3. Copy the app\controllers\sitecontroller.php controller to the app\modules\admin\controllers\sitecontroller.php and modify the login and exit actions, Other movements do not move. As follows:
Public functionActionlogin () {if(!\yii::$app->admin->isguest) { //return $this->gohome (); return $this->redirect (['/admin/site/index ']); } $model=NewLoginForm (); if($model->load (Yii::$app->request->post ()) &&$model-Login ()) { //return $this->goback (); return $this->redirect (['/admin/site/index ']); } return $this->render (' Login ', [ ' Model ' =$model, ]); } Public functionactionsignout () {/*Yii:: $app->user->logout (); return $this->gohome (); */ //Yii:: $app->user->logout (false);YII::$app->admin->logout (); //return $this->gohome (); return $this->redirect (['/admin/site/login ']); }
4. Copy the app\models\user.php to the App\modules\admin\models\ directory
5. Copy the app\models\loginform.php to the app\modules\admin\models\loginform.php directory and modify the contents as follows:
<?phpnamespace app\modules\admin\models; UseYii; UseYii\base\model; UseApp\modules\admin\models\adminuser;/** * LoginForm is the model behind the login form.*/classLoginFormextendsmodel{ Public $username; Public $password; Public $rememberMe=true; Private $_user=false; /** * @return array the validation rules. */ Public functionrules () {return [ //username and password are both required[[' Username ', ' password '], ' required '],//RememberMe must be a Boolean value[' RememberMe ', ' Boolean '],//password is validated by ValidatePassword ()[' Password ', ' ValidatePassword '], ]; } /** * Validates the password. * This method is serves as the inline validation for password. * * @param string $attribute The attribute currently being validated * @param array $params the additional Name-va Lue pairs given in the rule*/ Public functionValidatePassword ($attribute,$params) { if(!$this-hasErrors ()) { $user=$this-GetUser (); if(!$user|| !$user->validatepassword ($this-password)) { $this->adderror ($attribute, ' incorrect username or password. '); } } } /** * Logs in a user using the provided username and password. * @return Boolean whether the user is logged in successfully*/ Public functionLogin () {if($this-Validate ()) { returnYII::$app->admin->login ($this->getuser (),$this->rememberme? 3600*24*30:0); } return false; } /** * Finds user by [[username]] * * @return user|null*/ Public functionGetUser () {if($this->_user = = =false) { $this->_user = Adminuser::findbyusername ($this-username); } return $this-_user; }}
At this point after the front desk login is separated, the front desk login system has its own, here will not say
Front Desk login Address: Localhost/index.php?r=site/login
Front Exit Address: Localhost/index.php?r=site/logout
Background Login Address: Localhost/index.php?r=admin/site/login
Background exit Address: localhost/index.php?r=admin/site/singout
The 6 key methods are
1. Background to determine if login Yii:: $app->admin->isguest
2. Background login Yii:: $app->admin->login ($this->getuser (), $this->rememberme 3600*24*30:0);
3. Background exit Yii:: $app->admin->logout ();
4. The foreground determines whether to login Yii:: $app->user->isguest
5. Front Desk Login Yii:: $app->user->login ($this->getuser (), $this->rememberme 3600*24*30:0);
6. The front exit Yii:: $app->user->logout ();
Yii2 Base Template pre-and post-logon separation