Symfony路由区分歧义路径

Symfony routing to distinguish ambiguous paths

我遇到了 Symfony 路由问题。即使我在两个不同路由的路径中使用不同的参数,Symfony 也将其识别为一个模式并指向路由文件中首先定义的路径。 例如:

app_restaurants_inner:
    path:     /london-restaurants/{id}/{restaurant_name}.html
    defaults: { _controller: AppBundle:Restaurants:inner}

app_restaurants_by_cuisine:
    path:     /london-restaurants/cuisine/{cuisine}.html
    defaults: { _controller: AppBundle:Restaurants:index}

第一个路由加载一个特定的餐厅,参数是 id 和餐厅名称。餐厅名称仅包含 a-z、0-9 和连字符。 在第二个中,只有一个参数是 cuisine。但是,当我尝试加载美食(第二条路线)时,它会将我引导至与美食路径相似的餐厅路径。

另一方面,以下路线也被识别为类似于餐厅的路径。

app_restaurants_by_cuisine_letter:
    path:     /london-restaurants/cuisine/{cuisine}-{letter}.html
    defaults: { _controller: AppBundle:Restaurants:index}

单词 'cuisine' 被识别为“{id}”,“{cuisine}-{letter}”被识别为“{restaurant_name}”。

我该如何解决这个问题?

您应该在路由定义中添加一些要求Adding {wildcard} Requirements

app_restaurants_inner:
    path:     /london-restaurants/{id}/{restaurant_name}.html
    defaults: { _controller: AppBundle:Restaurants:inner}
    requirements:
        id: '\d+'

app_restaurants_by_cuisine:
    path:     /london-restaurants/cuisine/{cuisine}.html
    defaults: { _controller: AppBundle:Restaurants:index}