Developer1
Developer1

Reputation: 75

Symfony2 Routing parameters

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?

Upvotes: 2

Views: 110

Answers (1)

Fernando Caraballo
Fernando Caraballo

Reputation: 583

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

Upvotes: 3

Related Questions