类树的路由 [英] Routing for category 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)
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
这篇关于类树的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!