Yii returnUrl function

12,645

Solution 1

I have found a solution for my problem. I added this lines of code in login.php so after user login it will redirect on previous page:

if (Yii::app()->request->urlReferrer != 'http://www.example.com/user/login' && 
    Yii::app()->request->urlReferrer != 'http://www.example.com/user/register')
{
    Yii::app()->user->setReturnUrl(Yii::app()->request->urlReferrer);
}

Solution 2

Try this in order to keep track of the last visited valid url:

Add to you configuration:

'preload' => array(
    // preloading 'loginReturnUrlTracker' component to track the current return url that users should be redirected to after login
    'loginReturnUrlTracker'
),
'components' => array(
    'loginReturnUrlTracker' => array(
        'class' => 'application.components.LoginReturnUrlTracker',
    ),
    ...
),

Put this file in components/LoginReturnUrlTracker.php:

<?php

class LoginReturnUrlTracker extends CApplicationComponent
{

    public function init()
    {
        parent::init();

        $action = Yii::app()->getUrlManager()->parseUrl(Yii::app()->getRequest());

        // Certain actions should not be returned to after login
        if ($action == "site/error") {
            return true;
        }
        if ($action == "site/logout") {
            return true;
        }
        if ($action == "site/login") {
            return true;
        }

        // Keep track of the most recently visited valid url
        Yii::app()->user->returnUrl = Yii::app()->request->url;

    }

}
Share:
12,645
Irakli
Author by

Irakli

everyday i'm shuffling

Updated on June 04, 2022

Comments

  • Irakli
    Irakli almost 2 years

    Hello guys I have this code in main.php config file:

    'components' => array(
        '[.........]',
        'user'=>array(
            // enable cookie-based authentication
            'allowAutoLogin'=>true,
            'autoRenewCookie' => true,
            'returnUrl' => 'http://stackoverflow.com',
        )
    );
    

    My problem is that id doesn't redirects user to http://stackoverflow.com after login, can you please help me?

    UserController.php :

    public function actionLogin()
    {
        if (!Yii::app()->user->isGuest){
            $this->redirect('/user/index');
            return;
        }
    
        $model=new LoginForm;
    
        // if it is ajax validation request
        if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    
        // collect user input data
        if(isset($_POST['LoginForm']))
        {
            $model->attributes=$_POST['LoginForm'];
            // validate user input and redirect to the previous page if valid
            if($model->validate() && $model->login())
                $this->redirect(Yii::app()->user->returnUrl);
        }
        // display the login form
        $this->render('login',array('model'=>$model));
    }
    
  • Rohitashv Singhal
    Rohitashv Singhal almost 11 years
    If I want to add the dynamic login url instead of hard coded url, then how can I get this login url dynamically ?
  • trejder
    trejder over 9 years
    You should use !== instead of !=.
  • David Urry
    David Urry about 9 years
    Nice but is there a way to have it not be site dependent? It would be great to be able to deal with stage.example.com... and example.com. It seems that the setReturnUrl should be intelligent enough to return a Yii form and not just the urlReferrer.