dongmaopiao0901 2018-06-22 05:44
浏览 6

如果有多个连字符,则从路由获取变量

using symfony 3.0 where I want to get the ID from the URL.

  /**
   * @Route("/pathtocontent/{id}-{name}", defaults={"id"=null,"name"=null}, 
requirements={"name"="[a-zA-Z0-9\-_\/]+", "id"="\d+"}, name="routingname")
   * @Route("/pathtocontent/{name}-{id}", defaults={"id"=null,"name"=null}, 
requirements={"name"="[a-zA-Z0-9\-_\/]+", "id"="\d+"}, 
name="routingname_alias")
   */
  public function pathAction(Request $request, $id = null) {    
  }

So I have 2 rules for the same action. The URL can be:

/pathtocontent/Name-part-comes-here-1234

and also could be like that:

/pathtocontent/1234-Name-part-comes-here

The problem is the first case, where the name get all the: Name-part-comes-here-1234 value and the Id is null

How can I force the first case to parse the Id value out too, so I need to get the Id (1234) from the :

/pathtocontent/Name-part-comes-here-1234

  • 写回答

1条回答 默认 最新

  • duange051858 2018-06-22 06:01
    关注

    Use just:

    @Route("/pathtocontent/{name}/{id}", defaults={"id"=null,"name"=null},
    

    Because use have many hyphens (-)

    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等