Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple pattern in single symfony routing

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 }
like image 240
Justin John Avatar asked Jul 06 '12 13:07

Justin John


2 Answers

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.

like image 59
john Avatar answered Oct 18 '22 01:10

john


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");
*/
like image 27
René Höhle Avatar answered Oct 18 '22 01:10

René Höhle