Multiple pattern in single symfony routing

后端 未结 4 1197
萌比男神i
萌比男神i 2020-12-29 04:41

How to make multiple pattern in single Symfony routing?

Normally we have a routing as

blog:
    pattern:   /
    defaults:  { _controller: AcmeBlogB         


        
4条回答
  •  独厮守ぢ
    2020-12-29 04:59

    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");
    */
    

提交回复
热议问题