29

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

Dan Blows
  • 20,846
  • 10
  • 65
  • 96
gX.
  • 922
  • 1
  • 11
  • 18

3 Answers3

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

UPDATE:

In Symfony 3.2 you can do this:

// generating a URL with a fragment (/settings#password)
$this->redirectToRoute('user_settings', ['_fragment' => 'password']);

See https://symfony.com/blog/new-in-symfony-3-2-routing-improvements

Dan Blows
  • 20,846
  • 10
  • 65
  • 96
  • 1
    So.. you took my own answer and put your name on it? – gX. Sep 14 '14 at 12:05
  • I dont see your answer @gX. – Ascherer Oct 13 '15 at 17:25
  • http://stackoverflow.com/posts/9054865/revisions and http://stackoverflow.com/posts/5694244/revisions – gX. Oct 17 '15 at 15:24
  • @gX This is now a very old question and answer, and I was in my less experienced days on SO. I was probably going through unanswered questions and your one came up because your answer was posted as a comment _not_ an answer. [The correct etiquette for me would have been to post as community wiki](http://meta.stackoverflow.com/a/251598/582278) so I'm sorry for not doing that. From your side, in future make sure you post answers as answers, even if it's [an answer to your own question](http://stackoverflow.com/help/self-answer). Feel free to post another answer here, and I'll give you an upvote. – Dan Blows Oct 21 '15 at 13:56
  • 1
    Thanks for the reply, but as I had written in the original question: 'StackOverflow wouldn't let me post it as an answer since I'm a "new user"' – gX. Oct 23 '15 at 08:02
2

Similar but

$this->redirect('@routename?id='.$id.'#anchor7');

Works fine in Symfony 1.4 and seems slightly simpler solution

b.b3rn4rd
  • 8,494
  • 2
  • 45
  • 57
Tofuwarrior
  • 669
  • 9
  • 21
2

Using router :

$router->generateUrl('awesome_route') . '#comment-2';

This method is used by Fabien Potencier : https://github.com/symfony/symfony/issues/3910#issuecomment-5110238

Thomas Tourlourat
  • 137
  • 1
  • 1
  • 7