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

时间:2018-06-22 05:44:58

标签: php symfony

在我想从URL获取ID的地方使用symfony 3.0。

  /**
   * @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) {    
  }

因此,对于相同的操作,我有2条规则。网址可以是:

  

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

也可能是这样的:

  

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

问题是第一种情况,其中名称获得了所有条件: Name-part-comes-here-1234 值,且ID为

如何强制第一种情况也解析Id值,所以我需要从:

获取 Id (1234)
  

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

1 个答案:

答案 0 :(得分:0)

仅使用:

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

因为使用了许多hyphens(-)

相关问题