How to get the referer url in symfony redirection

12,280

I can suggest you to put the referer inside the session to be able to retrieve after your redirect. This how sfGuard perform it.

Before your redirect:

// save the referer
$referer = $this->getContext()->getActionStack()->getSize() > 1 ? $request->getUri() : $request->getReferer();
$this->getUser()->setAttribute('referer', $referer);

// make your redirect
$this->redirect('module2/action2');

Then, in your module2/action2, retrieve your referer like that:

$referer = $this->getUser()->getAttribute('referer');

// finally, remove it from session
$this->getUser()->getAttributeHolder()->remove('referer');
Share:
12,280
Thilanka
Author by

Thilanka

I'm a computer science undergraduate at Department of university of Moratuwa, Sri Lanka. I'm interested on image processing, web developing and the back end java programming.

Updated on June 04, 2022

Comments

  • Thilanka
    Thilanka almost 2 years

    I'm using symfony 1.4 for some web application. In there when someone try to access one action I'm redirecting it to a new action as follows.

    //Assume I'm redecting from module1/action1 to module2/action2
    $this->redirect('module2/action2');
    

    I in the module2/action2 can I get the url where it get redirected. I used

    $request->getReferer();
    

    But it doesn't give any result.

    I don't need to use forward instead of redirect because it doesn't match for my need. Can someone help me on this.

  • Thilanka
    Thilanka over 11 years
    So where do we need to do this. Is it at a filter or somewhere.
  • j0k
    j0k over 11 years
    Nope, you can put every thing in your action
  • Thilanka
    Thilanka over 11 years
    Yes but then every action we have to put. It won't be a generic place where we can refer.
  • Sébastien
    Sébastien almost 9 years
    extend your controller by a custom one where yo put this