How to set returnUrl value in Yii
Asked Answered
B

7

6

I am using Yii and the problem I am getting is with the Yii::app()->user->returnUrl. It always returns me to the index.php page.

How can I set its value to the page which requested the current page as I do not know from which page user has visited the current page?

Battery answered 30/11, 2013 at 10:46 Comment(0)
S
7

You can use Yii::app()->request->urlReferrer to see where the user came from.

public function beforeAction()
{
    Yii::app()->user->returnUrl = Yii::app()->request->urlReferrer;
    return parent::beforeAction();
}

Be careful, if the user came from a 3rd party site, then this will redirect them away from your site.

Schnozzle answered 30/11, 2013 at 11:24 Comment(2)
Thanks friend...and thanks for warning me about this too :) cheersBattery
There is a typo here: Yii::app()->**user**->urlReferrer should be Yii::app()->**request**->urlReferrer.Choriamb
E
6

For those using Yii2

Yii::$app->user->returnUrl = Yii::$app->request->referrer;
Etui answered 9/2, 2015 at 11:1 Comment(0)
P
3

There is no such thing as: Yii::app()->user->urlReferrer

It should be: Yii::app()->request->urlReferrer or Yii::app()->request->requestUri (current page)

So try this:

Yii::app()->user->returnUrl = Yii::app()->user->urlReferrer;

Or this one (which I personally prefer):

Yii::app()->user->returnUrl = Yii::app()->request->requestUri;
Passacaglia answered 21/7, 2014 at 14:5 Comment(0)
A
2

I created a Yii extension to manage return URLs, you can find it here: https://github.com/cornernote/yii-return-url#yii-returnurl

It's an improvement over the way Yii handles it because it stores the return url in the GET/POST data instead of the SESSION. This means that if you have multiple tabs open then each can have it's own return url.

Aleron answered 30/5, 2015 at 10:12 Comment(0)
M
0

You can use like this

$http = new CHttpRequest();
$referrer_url = $http->getUrlReferrer();
$this->redirect($referrer_url);

Hope this will help you

Manfred answered 30/11, 2013 at 11:3 Comment(1)
Am I missing something, or you're wrong? If you read getUrlReferrer from fresh, newly created CHttpRequest it will be... empty? You should most definitely use the one provided by application object (Yii::app()->request->urlReferrer), not create new one. But... maybe I'm not getting the whole picture and I'm wrong...Imaginative
A
0

You can also do like that

Yii::app()->user->returnUrl = Yii::app()->request->urlReferrer;
$this->redirect(Yii::app()->user->returnUrl);
Assemble answered 22/4, 2016 at 4:44 Comment(0)
M
0

For those who still struggling with returning to previous page: You have to create a property in login model $referer, and at the time when you initialize your Login model you set this property to Yii::$app->request->referrer. You have to go this way because after you submit your form your referrer does change to current page, and since it is null, it return you to index action. So the way how to pass the return URL is to store it in the hidden field at the form, so when you submit the form you have this URL loaded and you can perform action to return $this->goBack($form->referer ? $form->referer : null). Here is the code:

Login model:

public $referer;

Controller action:

$model = new Login();
$model->referer = Yii::$app->request->referrer;
...
if($model->load(Yii::$app->request->post()){
    ...
    if($model->save()){
        return $this->goBack((($model->referer) ? $model->referer : null));
    }
}

View login.php:

<?= $form->field($model, 'referer')->hiddenInput()->label(false) ?>
Matutinal answered 5/6, 2019 at 3:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.