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 }
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.
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");
*/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With