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

3 Answers 3

65
$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

Sign up to request clarification or add additional context in comments.

5 Comments

So.. you took my own answer and put your name on it?
I dont see your answer @gX.
@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 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. Feel free to post another answer here, and I'll give you an upvote.
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"'
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

Comments

2

Similar but

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

Works fine in Symfony 1.4 and seems slightly simpler solution

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.