码迷,mamicode.com
首页 > 其他好文 > 详细

Yii2 基础模板前后台登录分离

时间:2016-03-10 23:38:58      阅读:542      评论:0      收藏:0      [点我收藏+]

标签:

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 并修改其中的登录和退出动作,其他动作不动。如下:
    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;
    }
}
至此后前台的登录就分离了,前台的登录系统有自带的,这里就不说了

前台登录地址: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();



Yii2 基础模板前后台登录分离

标签:

原文地址:http://www.cnblogs.com/ser0632/p/5263790.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!