Yii Framework Forum: Any 'yii' way to get the previous URL? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Any 'yii' way to get the previous URL? Rate Topic: -----

#1 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,049
  • Joined: 30-October 09

Posted 27 September 2010 - 11:57 AM

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?
0

#2 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 27 September 2010 - 12:01 PM

Maybe use CWebUser::$returnUrl?

See this extension, it sets the returnUrl automatically.
0

#3 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,049
  • Joined: 30-October 09

Posted 27 September 2010 - 12:14 PM

What I'm after has nothing to do with users/login :)
0

#4 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

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>

0

#5 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 27 September 2010 - 12:31 PM

If you want to use the actual referer, you may extend this method:

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").
0

#6 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

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:

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
0

#7 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,017
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 28 September 2010 - 02:22 AM

Keep in mind, sessions are not "multi-tab-safe"/"multi-window-safe". Some time ago i've written an extension that addresses this problem. It uses a URL parameter to store, where your user came from. Check it out here.
1

#8 User is offline   Dark-Sun 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 04-April 12
  • Location:Ahwaz

Posted 29 June 2012 - 08:33 AM

*
POPULAR

it's an outdated topic, but you may try this one:
Yii::app()->request->urlReferrer


see CHttpRequest
9

#9 User is offline   kalempir 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 50
  • Joined: 20-July 11
  • Location:Kyrgyzstan

Posted 15 March 2013 - 07:55 AM

View PostDark-Sun, on 29 June 2012 - 08:33 AM, said:

it's an outdated topic, but you may try this one:
Yii::app()->request->urlReferrer


see CHttpRequest


Thanks, it helped me.
2

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users