zoukankan      html  css  js  c++  java
  • Yii2 基础模板前后台登录分离

    1.用GII 生成一个模块(modules)名字为 admin

    2.在./config/web.php 中加入如下配置

        'modules' => [
            'admin' => [
                    'class' => 'appmodulesadminModule',//后台模块
            ]
        ],    
        'components' => [
     
            'user' => [
                'identityClass' => 'appmodelsUser',
                'enableAutoLogin' => true,
                'loginUrl'=>['/site/login'],//定义后台默认登录界面[权限不足跳到该页]
                'identityCookie' => ['name' => '__user_identity', 'httpOnly' => true],
                'idParam' => '__user'
            ],
            
            //前后台登录分离
            'admin' => [
                'class' => 'yiiwebUser',
                'identityClass' => 'appmodulesadminmodelsAdminUser',
                'enableAutoLogin' => true,
                'loginUrl'=>['/admin/site/login'],//定义后台默认登录界面[权限不足跳到该页]
                'identityCookie' => ['name' => '__admin_identity', 'httpOnly' => true],
                'idParam' => '__admin'
            ],
    ]
    3.把 appcontrollersSiteController.php 控制器复制到 appmodulesadmincontrollersSiteController.php 并修改其中的登录和退出动作。
    SiteController.php的命名空间改为 namespace appmodulesadmincontrollers;

       把 use appmodelsLoginForm; 改为 use appmodulesadminmodelsLoginForm;
       把 use appmodelsContactForm; 改为  use appmodulesadminmodelsContactForm;

        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.把 appmodelsUser.php 复制到 appmodulesadminmodels目录下

    5.把 appmodelsLoginForm.php 复制到 appmodulesadminmodelsLoginForm.php目录下,并修改内容,如下:
    <?php
    
    namespace appmodulesadminmodels;
    
    use Yii;
    use yiiaseModel;
    use appmodulesadminmodelsAdminUser;
    
    /**
     * 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.把 viewssite文件夹复制到 adminviews 中
    在 adminviewindex.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();



  • 相关阅读:
    阿里云Ubuntu环境搭建Docker服务
    Cocos2d-x手机游戏开发中-组合动作
    Java中将时间戳转化为Date类型
    Ubuntu14.04+eclipse下cocos2d-x3.0正式版环境的搭建
    hdu 4901 The Romantic Hero(dp)
    scikit-learn:3.4. Model persistence
    桥接模式和NAT模式差别
    JavaScript入门:004—JS凝视的写法和基本运算符
    MySQL 创建用户 与 授权
    【观点见解】解读大数据的5个误区
  • 原文地址:https://www.cnblogs.com/ser0632/p/5263790.html
Copyright © 2011-2022 走看看