Justin John Justin John - 26 days ago 7
PHP Question

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 }

Answer

The easiest way is to dublicate 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 check if index is set.

Edit:

If you work with annotations what i prefer then you can write more then one route over your class something like this:

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