Symfony2 how to allow slug with dashes in routes regex?

后端 未结 3 608
旧时难觅i
旧时难觅i 2020-12-16 14:54

My route (slug contains dashes!):

region:
  pattern: /regione/{slug}-{id}
  defaults: { _controller: SWAItaliaInCifreBundle:Default:region }
<
相关标签:
3条回答
  • 2020-12-16 15:12

    Slashes are by default forbidden. You can enable them by changing the default requirements. In your case it'd be also good to give requirements for the id as it's separated with dash.

    See example below.

    region:
        pattern: /regione/{slug}-{id}
        defaults:
            _controller: SWAItaliaInCifreBundle:Default:region
        requirements:
            slug: "[a-zA-Z1-9\-_\/]+"
            id: "\d+"
    
    0 讨论(0)
  • 2020-12-16 15:26

    This regex works for me. ({id} requirement suggested by Michael)

    region:
      pattern: /regione/{slug}-{id}
      defaults: { _controller: SWAItaliaInCifreBundle:Default:region }
      requirements:
        slug: "[a-zA-Z0-9-_/]+"
        id: "\d+"
    
    0 讨论(0)
  • 2020-12-16 15:29

    if you try this it will throw a error like this:

    An exception has been thrown during the rendering of a template ("Parameter "slug" for route "routing_whatever" must match "[a-zA-Z0-9-_/]+" ("Topics/Virtualization Security" given).") in ...
    

    as viewed in http://symfony.com/doc/current/cookbook/routing/slash_in_parameter.html you must use:

    slug: ".+"

    0 讨论(0)
提交回复
热议问题