Routing for category tree

2019-04-01 01:50发布

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

/cat/subcat1/subcat2/subcat3

I can do it defining routes like

/{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?

2条回答
Summer. ? 凉城
2楼-- · 2019-04-01 02:33
We Are One
3楼-- · 2019-04-01 02:36

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: ".+"

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 !

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

查看更多
登录 后发表回答