Wednesday, May 16, 2012

Anchor in URL when using Symfony"s redirect function


I'm using




$this->redirect('route', array('id' => $id));



but I need to be able to put "#" anchor at the end but I can't find a way of doing that. Any ideas?



The code




$this->redirect('route', array('id' => $id));



returns /route/id/5 but I want to be able to create /route/id/5#anchor7



EDIT - Solution found

Ok figured out a solution. Its working fine :)




$this->redirect($this->generateUrl('route', array('id' => $id)) . '#anchor7');



StackOverflow wouldn't let me post it as an answer since I'm a "new user" O_o


Source: Tips4all

2 comments:

  1. Your own answer:

    $this->redirect($this->generateUrl('route', array('id' => $id)) . '#anchor7');

    ReplyDelete
  2. not tested, but:

    $this->redirect('route', array('id' => ($id.'#anchor7'));

    Another (hacky) way:

    Add a field to your parameters, e.g. 'anchor' => 'anchor7' and on the target site, parse this parameter and use javascript to scroll to the anchor.

    ReplyDelete