dongshao6591 2017-08-09 09:23
浏览 45
已采纳

Symfony 3路由允许在占位符值中使用连字符/短划线

I am having an issue with a route in Symfony, I have a route setup that needs to match the below:

/my-test-route-holidays/

The above "my-test-route" is the placeholder variable.

The route in symfony is as follows:

overview:
  path: /{var}-holidays/
  defaults: { _controller: AppBundle:Overview:index }

Symfony cannot find the route, a route like below does work without dashes/hyphens in the variable:

/test-holidays/

So my question is, how can I allow hyphens inside a route placeholder?

Thanks

  • 写回答

1条回答 默认 最新

  • dpvm7231 2017-08-15 15:31
    关注

    I have managed to solve this myself, it was a quick skim of the documentation that led me to the wrong answer.

    I come across this page on Symfonys website a few times whilst trying to research the answer: Symfony Docs Current Slash in Parameter

    In their example:

    share:
        path:     /share/{token}
        defaults: { _controller: AppBundle:Default:share }
        requirements:
            token: .+
    

    You can see that they have added "requirements" and underneath that "token", I just assumed that "token" was something to do with regex but actually it relates to the placeholder you have in your "path" and they should match.

    Below is what I had:

    overview:
      path: /{var}-holidays/
      defaults: { _controller: AppBundle:Overview:index }
      requirements:
          token: .+
    

    But what I actually needed was to replace the "token" under "requirements" with "var".

    overview:
      path: /{var}-holidays/
      defaults: { _controller: AppBundle:Overview:index }
      requirements:
          var: .+
    

    And what do you know, it works!

    I hope somebody else finds this useful.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料