2

I am new to symfony2 and MVC in general. I am going through the documentation for Symfony and I am in the chapter on Routing.

I am getting confused with the annotation

/**
 * @Route("/blog/{slug}", name="blog_show")
 */
public function showAction($slug)
{
    // ...
}

I understand that if user visits blog/xxx, the showAction will be called. What I do not understand is why there is name="blog_show" after the comma in the @Route.

Could someone please describe why we use it?

1
  • Please accept it as valid if you think that was useful :) Commented Jul 10, 2015 at 11:00

1 Answer 1

3

Is just an Alias for this route

This name is the one that you have to call for example from twig

<a href="{{ path('blog_show', {'slug': my-blog-post}) }}" ... 

It will call /blog/my-blog-post

Or if you want to redirect to this address

return new RedirectResponse($this->generateUrl('blog_show'), array('slug' => 'my-blog-post'));

Or generate URL

$url = $this->generateUrl('blog_show', array('slug' => 'my-blog-post'));

Here you have the documentation

http://symfony.com/doc/current/book/controller.html#redirecting

http://symfony.com/doc/current/book/templating.html#linking-to-pages

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

1 Comment

Cool, thanks, that seems to make sense. I can't test it out right now but seems to be right judging by the docs. I will mark it as valid.

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.