Yii 将管理员和经过身份验证的用户重定向到所需的页面 [英] Yii redirecting the admin and authenticated user to the desired page

查看:28
本文介绍了Yii 将管理员和经过身份验证的用户重定向到所需的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 yii 的新手.我希望我的管理员在从 webapp/user/login 登录时重定向到我想要的页面,即 localhost/webapp/story 现在它正在将我重定向到 index.php.

我还注册了一个用户,并为该用户提供了一个经过身份验证的角色,我希望当我的用户(经过身份验证的用户)通过 webapp/user/login 登录时,该用户将被重定向到 index.php.

所以有两件事:

<代码>1.将管理员重定向到所需的页面,即 webapp/story.2. 将经过身份验证的用户重定向到 index.php.

我正在使用 yii 用户和正确的扩展名.请帮我解决一下这个.代码 LoginController 如下:

user->isGuest) {$model=新用户登录;//收集用户输入数据if(isset($_POST['U​​serLogin'])){$model->attributes=$_POST['U​​serLogin'];//验证用户输入并在有效时重定向到上一页if($model->validate()) {$this->lastViset();if (Yii::app()->user->returnUrl=='/index.php')$this->redirect(Yii::app()->controller->module->returnUrl);else//yehen par kuch aye ga 根据$this->redirect(Yii::app()->user->returnUrl);}}//显示登录表单$this->render('/user/login',array('model'=>$model));} 别的$this->redirect(Yii::app()->controller->module->returnUrl);}私有函数 lastViset() {$lastVisit = User::model()->notsafe()->findByPk(Yii::app()->user->id);$lastVisit->lastvisit = time();$lastVisit->save();}}

解决方案

我认为可能是这样的

user->isGuest) {$model=新用户登录;//收集用户输入数据if(isset($_POST['U​​serLogin'])){$model->attributes=$_POST['U​​serLogin'];//验证用户输入并在有效时重定向到上一页if($model->validate()) {$this->lastViset();//旧代码注释//if (Yii::app()->user->returnUrl=='/index.php')//$this->redirect(Yii::app()->controller->module->returnUrl);//else//yehen par kuch aye ga 根据//$this->redirect(Yii::app()->user->returnUrl);//新代码如果 (UserModule::isAdmin()){$this->redirect(array('story/index'));}别的  {$this->redirect(Yii::app()->user->returnUrl);}}}//显示登录表单$this->render('/user/login',array('model'=>$model));} 别的$this->redirect(Yii::app()->controller->module->returnUrl);}私有函数 lastViset() {$lastVisit = User::model()->notsafe()->findByPk(Yii::app()->user->id);$lastVisit->lastvisit = time();$lastVisit->save();}}

I am new to yii. I want my admin upon login from webapp/user/login to redirect to the page I want which is localhost/webapp/story right now it is redirecting me to the index.php.

I have also registered a user and given that user a role which is authenticated and I want that when my user(the authenticated user) logs in via webapp/user/login then that user is redirected to index.php.

so there are two things:

1. redirecting admin to the desired page which is webapp/story.
2. redirecting the authenticated user to index.php.

I am using yii user and right extension. Please help me with this. The code LoginController is below:

<?php

class LoginController extends Controller
{
    public $defaultAction = 'login';

    /**
     * Displays the login page
     */
    public function actionLogin()
    {
    if (Yii::app()->user->isGuest) {
        $model=new UserLogin;
        // collect user input data
        if(isset($_POST['UserLogin']))
        {
            $model->attributes=$_POST['UserLogin'];
            // validate user input and redirect to previous page if valid
            if($model->validate()) {
                $this->lastViset();
                if (Yii::app()->user->returnUrl=='/index.php')
                        $this->redirect(Yii::app()->controller->module->returnUrl);
                else// yehen par kuch aye ga according
                    $this->redirect(Yii::app()->user->returnUrl);
            }
        }
        // display the login form
        $this->render('/user/login',array('model'=>$model));
        } else
        $this->redirect(Yii::app()->controller->module->returnUrl);
     }

    private function lastViset() {
    $lastVisit =     User::model()->notsafe()->findByPk(Yii::app()->user->id);
    $lastVisit->lastvisit = time();
    $lastVisit->save();
    }

 }   

解决方案

I think could be somethings like this

<?php

class LoginController extends Controller
{
    public $defaultAction = 'login';

    /**
     * Displays the login page
     */
    public function actionLogin()
    {
    if (Yii::app()->user->isGuest) {
        $model=new UserLogin;
        // collect user input data
        if(isset($_POST['UserLogin']))
        {
            $model->attributes=$_POST['UserLogin'];
            // validate user input and redirect to previous page if valid
            if($model->validate()) {

                $this->lastViset();

                // Old code commentede
                //if (Yii::app()->user->returnUrl=='/index.php')
                //        $this->redirect(Yii::app()->controller->module->returnUrl);
                //else// yehen par kuch aye ga according
                //    $this->redirect(Yii::app()->user->returnUrl);

                // new code
                if (UserModule::isAdmin()){
                    $this->redirect(array('story/index'));
                }  
                else  {
                    $this->redirect(Yii::app()->user->returnUrl);   
                }  


            }
        }
        // display the login form
        $this->render('/user/login',array('model'=>$model));
        } else
        $this->redirect(Yii::app()->controller->module->returnUrl);
     }

    private function lastViset() {
    $lastVisit =     User::model()->notsafe()->findByPk(Yii::app()->user->id);
    $lastVisit->lastvisit = time();
    $lastVisit->save();
    }

 }   

这篇关于Yii 将管理员和经过身份验证的用户重定向到所需的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆