Multiple pattern in single symfony routing

后端 未结 4 1208
萌比男神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 05:19

    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
    

提交回复
热议问题