Yii redirecting the admin and authenticated user t

2019-06-01 10:06发布

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();
    }

 }   

标签: yii
1条回答
劳资没心,怎么记你
2楼-- · 2019-06-01 11:06

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();
    }

 }   
查看更多
登录 后发表回答