How to get previous page route in Symfony?

VitalyP picture VitalyP · Apr 1, 2011 · Viewed 15.5k times · Source

I am looking for way to do this in 'right' symfony way.

Answer

Pabloks picture Pabloks · Apr 2, 2011

There's a way to get the referer page from the $request variable. For example, if I was in myaction/mypage and click to myaction2/mypage2 by this getReferer() method I get 'http://myweb/myaction/mypage'.

If you are in an action method this can be done by

public function executeMyaction(sfWebRequest $request)
{
   $previousUrl = $request->getReferer();
   ...
}

if you are somewhere else you can get the request by getting the conext

$previousUrl = $this->getContext()->getRequest()->getReferer();

For for sfWebRequest methods check the sfWebRequest API.

Note: this value could be inaccesible using proxy's