类别树的路由

2022-01-16 00:00:00 url-routing php symfony doctrine

我正在使用 Tree 学说类别树的扩展,并希望有如下路线:

I am using the Tree doctrine extension for a category tree and would like to have routes like:

/cat/subcat1/subcat2/subcat3

我可以这样定义路线

/{cat}
/{cat}/{subcat}
/{cat}/{subcat}/{subcat2)
etc...

但是有没有更优雅和通用的方法来实现它?一个可以接受无限数量的关卡的系统?

But is there a more elegant and general way of implementing this? A system that can accept an unlimited number of levels?

推荐答案

你可以做的是在你的路由参数中接受斜线(仅适用于这个路由).它涉及您不能将任何其他参数排队,因为斜线分隔符将被视为类别参数的一部分...

What you can do is accepting slashes in your routing parameters (for this route only). It involves that you can't queue any other parameter as slash separator will be seen as being part of category parameter...

那么,如何管理路由参数中的斜线:

So, how to manage slashes in a routing parameter :

_hello:
    pattern: /category/{category}
    defaults: { _controller: AcmeDemoBundle:Demo:category }
    requirements:
        category: ".+"

调用 /category/cat1/sub1/sub2 将调用 DemoController::categoryAction($category) 方法,并将 'cat1/sub1/sub2' 作为 $category 参数.只需使用您自己的代码来解码!

Calling /category/cat1/sub1/sub2 will call DemoController::categoryAction($category) method with 'cat1/sub1/sub2' as $category parameter. Just use your own code to decode !

在官方文档中找到的代码示例:http://symfony.com/doc/2.0/cookbook/routing/slash_in_parameter.html

Code sample found on official doc : http://symfony.com/doc/2.0/cookbook/routing/slash_in_parameter.html

相关文章