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.

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

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集