1.用GII 生成一个模块(modules)名字为 admin
2.在./config/web.php 中加入如下配置
'modules' => [ 'admin' => [ 'class' => 'app\modules\admin\Module',//后台模块 ] ], 'components' => [ 'user' => [ 'identityClass' => 'app\models\User', 'enableAutoLogin' => true, 'loginUrl'=>['/site/login'],//定义后台默认登录界面[权限不足跳到该页] 'identityCookie' => ['name' => '__user_identity', 'httpOnly' => true], 'idParam' => '__user' ], //前后台登录分离 'admin' => [ 'class' => 'yii\web\User', 'identityClass' => 'app\modules\admin\models\AdminUser', 'enableAutoLogin' => true, 'loginUrl'=>['/admin/site/login'],//定义后台默认登录界面[权限不足跳到该页] 'identityCookie' => ['name' => '__admin_identity', 'httpOnly' => true], 'idParam' => '__admin' ], ]
3.把 app\controllers\SiteController.php 控制器复制到 app\modules\admin\controllers\SiteController.php 并修改其中的登录和退出动作。
SiteController.php的命名空间改为 namespace app\modules\admin\controllers;
把 use app\models\LoginForm; 改为 use app\modules\admin\models\LoginForm;
把 use app\models\ContactForm; 改为 use app\modules\admin\models\ContactForm;
public function actionLogin() { if (!\Yii::$app->admin->isGuest) { //return $this->goHome(); return $this->redirect(['/admin/site/index']); } $model = new LoginForm(); 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 function actionSignout() { /* 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.把 app\models\User.php 复制到 app\modules\admin\models\目录下
5.把 app\models\LoginForm.php 复制到 app\modules\admin\models\LoginForm.php目录下,并修改内容,如下:
<?php namespace app\modules\admin\models; use Yii; use yii\base\Model; use app\modules\admin\models\AdminUser; /** * LoginForm is the model behind the login form. */ class LoginForm extends Model { public $username; public $password; public $rememberMe = true; private $_user = false; /** * @return array the validation rules. */ public function rules() { 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 serves as the inline validation for password. * * @param string $attribute the attribute currently being validated * @param array $params the additional name-value pairs given in the rule */ public function validatePassword($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 function login() { if ($this->validate()) { return Yii::$app->admin->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0); } return false; } /** * Finds user by [[username]] * * @return User|null */ public function getUser() { if ($this->_user === false) { $this->_user = AdminUser::findByUsername($this->username); } return $this->_user; } }
6.把 views\site文件夹复制到 admin\views\ 中
在 admin\view\index.php中加入
<?=Yii::$app->admin->isGuest ? Html::a('Login',['/admin/site/login']) : Html::a('logout('.Yii::$app->admin->identity->username.')',['/admin/site/signout'])?>
这样后台就有显示登录及退出连接
至此后前台的登录就分离了,前台的登录系统有自带的,这里就不说了
前台登录地址:localhost/index.php?r=site/login
前台退出地址:localhost/index.php?r=site/logout
后台登录地址:localhost/index.php?r=admin/site/login
后台退出地址:localhost/index.php?r=admin/site/singout
关键的 6 个方法是
1.后台判断是否登录 Yii::$app->admin->isGuest
2.后台登录 Yii::$app->admin->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
3.后台退出 Yii::$app->admin->logout();
4.前台判断是否登录 Yii::$app->user->isGuest
5.前台登录 Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
6.前台退出 Yii::$app->user->logout();