On most of my pages I have a "back to previous" link. Now the user can actually end up on a page via several different routes. I know I can use standard PHP $_SERVER['HTTP_REFERER'] to get the correct URL but I was wondering if Yii includes any method to do this?
Page 1 of 1
Any 'yii' way to get the previous URL?
#4
Posted 27 September 2010 - 12:23 PM
You can set $returnUrl anywhere and for any reason, it's not limited to login. Then on your content page, simply do something like:
<a href="<?php echo Yii::app()->user->returnUrl; ?>">Return to previous page</a>
#5
Posted 27 September 2010 - 12:31 PM
If you want to use the actual referer, you may extend this method:
Now in your view/controller you can check whether Yii::app()->request->getUrlReferrer(true) is null (means not possible to display "return to previous page").
class HttpRequest extends CHttpRequest { public function getUrlReferrer($internalOnly = false) { $referrer = parent::getUrlReferrer(); if ($internalOnly) { // Check if $referrer is an internal referer, if not set it to null } return $referrer; } }
Now in your view/controller you can check whether Yii::app()->request->getUrlReferrer(true) is null (means not possible to display "return to previous page").
#6
Posted 28 September 2010 - 01:32 AM
There are some browser that can be instructed to not to send or send a fake referer.
Better follow the first advice of Y!!. You can implement a behaviour for your application, and in beforRequest, for example, you can set the CWebuser::returnUrl (or, if you don't like this name, you can set wherever you want in the session the return url) and then use this value for the back button.
You can also save an array instead of a single value (in this case you are forced to use something in session, for example a new var named path_in_the_site). You can send a variable with the back button and do something nice like this:
This allows you to do more than one back, following backward all the path the user did
Better follow the first advice of Y!!. You can implement a behaviour for your application, and in beforRequest, for example, you can set the CWebuser::returnUrl (or, if you don't like this name, you can set wherever you want in the session the return url) and then use this value for the back button.
You can also save an array instead of a single value (in this case you are forced to use something in session, for example a new var named path_in_the_site). You can send a variable with the back button and do something nice like this:
public function beforeRequest() { if (isset($_GET['back'])) //if has been pressed the back button { unset Yii::app()->user->path_in_the_site[sizeof(Yii::app()->user->path_in_the_site-1)]; // delete the last visited page } else { Yii::app()->user->path_in_the_site[]= [...] } }
This allows you to do more than one back, following backward all the path the user did
#9
Share this topic:
Page 1 of 1