Routing for category tree(类别树的路由)
问题描述
我正在使用 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
这篇关于类别树的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:类别树的路由
基础教程推荐
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何替换eregi() 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
