Multiple pattern in single symfony routing

29,986

Solution 1

Are you using Symfony2? If you are and can use annotations for your routing instead of yml or xml then it's possible to have multiple routes defined along these lines:

/**
* @Route("/");
* @Route("/home");
*/

Then you don't need to duplicate the action method.

Solution 2

The easiest way is to duplicate the block and make 2 routes.

blog:
    pattern:   /
    defaults:  { _controller: AcmeBlogBundle:Blog:index, page: 1 }

blog_index:
    pattern:   /index
    defaults:  { _controller: AcmeBlogBundle:Blog:index, page: 1 }

So you have the possibility to use both of them in your path if you need it.

Here you can see another post how to use regex in your routing. Perhaps you can write a simple regex, which checks whether index is set.

Edit:

If you work with annotations, which I prefer, then you can write more than one route over your Controller's Action method. Something like this:

/**
* @Route("/");
* @Route("/home");
*/

Solution 3

When using YAML routes you can also use the node anchors expression syntax to reference an existing route definition.

& specifies the first occurrence of an anchor, * specifies the anchor to reference, << tells the Symfony yaml parser to merge the specified node.

blog: &blog
  path: /
  defaults:  { _controller: AcmeBlogBundle:Blog:index, page: 1 }

blog_index:
  <<: *blog
  path: /index

blog_page:
  <<: *blog
  path: /blog

Alternatively you can use anchors on a route attribute value.

blog:
  path: /
  defaults: &blog_defaults
    _controller: AcmeBlogBundle:Blog:index
    page: 1

blog_index:
  path: /index
  defaults: *blog_defaults

blog_page:
  path: /blog
  defaults: *blog_defaults

However to prevent poor SEO due to duplicate content, it is recommended to use a redirect instead.

blog:
  path: /
  defaults: { _controller: AcmeBlogBundle:Blog:index, page: 1 }

blog_index:
  path: /index
  defaults: &blog_redirect
    _controller: FrameworkBundle:Redirect:redirect
    route: blog
    permanent: true

blog_page:
  path: /blog
  defaults: *blog_redirect

Solution 4

Just to add to john's answer:

I use it a lot with FOSJsRoutingBundle:

/**
 * @Route("/", name="route_name_1", options={"expose"=true})
 * @Route("/{id}", name="route_name_2", options={"expose"=true})
 * @Method("GET")
 * @Template()
 */

This way I have one method and two routes.

Just remember to set default $id value:

public function indexAction($id = null)
{
   ...
}
Share:
29,986

Related videos on Youtube

Justin John
Author by

Justin John

An enthusiastic kid from software development, always looking to learn new web technologies, both on my own and from others. Need help of my fellow community members to do so... LinkedIn profile Github profile Twitter feed

Updated on December 31, 2020

Comments

  • Justin John
    Justin John over 3 years

    How to make multiple pattern in single Symfony routing?

    Normally we have a routing as

    blog:
        pattern:   /
        defaults:  { _controller: AcmeBlogBundle:Blog:index, page: 1 }
    

    Is it possible to have two routing patterns?

    Something like

    blog:
        #Below pattern to match with '/' or '/index'    
        pattern:   {/ , /index}  
        defaults:  { _controller: AcmeBlogBundle:Blog:index, page: 1 }
    
  • Jean-Luc Barat
    Jean-Luc Barat almost 8 years
    Does it work with different parameters ? If yes how ?
  • Federico Ponzi
    Federico Ponzi over 6 years
    it's 2017: is there a Symfony3 way to do this with routing.yaml?
  • Leevi Graham
    Leevi Graham about 6 years
    Nice… Didn't know about anchors before this post.
  • Anjana Silva
    Anjana Silva over 5 years
    @LeeviGraham count me in too :D