ZF2多级树形路由Route配置实例
来源:互联网 发布:php需要用到什么软件 编辑:程序博客网 时间:2024/05/04 13:43
Zend Framework 2的路由设置非常灵活,每一个路由都可以呈现树形结构,同时还可以为一个路由及其子路由设置不同的Controller与Action。在此之上又提供了非常丰富的路由种类,查阅ZF2路由相关文档可以了解到路由的常用方法。
虽然可供选择的余地很大,不过在实际使用中,Zend\Mvc\Router\Http\Segment应该是适用范围最广的,推荐使用。因为在实际项目中,经常需要处理URL末尾的斜线/问题。比如 http://avnpc.com/life 与 http://avnpc.com/life/ 应该指向同一处,此时如果用Zend\Mvc\Router\Http\Literal,就需要对有斜线和无斜线两种情况分别写路由:
'router' => array( 'routes' => array( 'life' => array( 'type' => 'literal', 'options' => array( 'route' => '/life', 'defaults' => array( 'controller' => 'LifeController', 'action' => 'index', ), ), ), 'lifeslash' => array( 'type' => 'literal', 'options' => array( 'route' => '/life/', 'defaults' => array( 'controller' => 'LifeController', 'action' => 'index', ), ), ), ),),
使用Segment路由则只需配置一次:
'router' => array( 'routes' => array( 'life' => array( 'type' => 'segment', 'options' => array( 'route' => '/life[/]', 'defaults' => array( 'controller' => 'LifeController', 'action' => 'index', ), ), ), ),),
那么下面用一个比较复杂的实例来说明如何在ZF2中配置一个多级树形路由Route。举例一个在微博或SNS系统中都比较常见的应用场景,我们希望项目的URL中:
- /user 对应用户列表页面
- /user/:user_id 对应用户的个人主页,比如 /user/AlloVince 就对应AlloVince用户的个人主页
- /user/:user_id/blog/ 对应用户的博客列表页面,比如/user/AlloVince/blog 就会列出AlloVince写过的Blog
- /user/:user_id/blog/:blog_id 对应用户的一篇博客文章
那么最终写成Route应该是这样的:
'router' => array( 'routes' => array( 'user' => array( 'type' => 'Segment', 'options' => array( 'route' => '/user[/]', 'defaults' => array( 'controller' => 'UserController', 'action' => 'index', ), ), 'may_terminate' => true, 'child_routes' => array( 'profile' => array( 'type' => 'Segment', 'options' => array( 'route' => '[:id][/]', 'constraints' => array( 'id' => '[a-zA-Z0-9_-]+' ), 'defaults' => array( 'action' => 'get' ), ), 'may_terminate' => true, 'child_routes' => array( 'blog' => array( 'type' => 'Segment', 'options' => array( 'route' => 'blog[/]', 'constraints' => array( ), 'defaults' => array( 'action' => 'blog' ) ), 'may_terminate' => true, 'child_routes' => array( 'post' => array( 'type' => 'Segment', 'options' => array( 'route' => '[:post_id][/]', 'constraints' => array( 'post_id' => '[a-zA-Z0-9_-]+' ), 'defaults' => array( 'action' => 'post' ) ), 'may_terminate' => true, ), ), ), ), //profile child_routes end ), //profile end ), //user child_routes end ), //user end ),),
需要注意的是may_terminate这个参数,代表路由是否可以被终止,如果没有这个参数的话,路由在匹配到父级URL时会继续向下匹配子路由。比如我们去掉上例的第一个may_terminate。然后访问/user。则会继续进入到profile子路由然后抛出错误。
0 0
- ZF2多级树形路由Route配置实例
- ZF2多级树形路由Route配置实例
- linux路由route配置
- 策略路由以及使用 ip route , ip rule , iptables 配置策略路由实例
- zf2 路由跳转方法
- zf2的路由
- angular js ng-route 路由简单实例
- Route-Policy 路由策略 实例详解
- linux 路由配置——route命令
- route 路由配置 命令
- linux路由配置命令route学习
- NG之Route模块路由配置
- angularjs路由配置$route以及案例展示
- route 路由
- route路由
- 54、实例 jQuery设计树形结构(类似多级菜单)
- angularJS系列之监听路由变化$location和$route实例
- vue-route 配置实例干货版
- linux中为什么普通用户可以修改自己的密码哪?
- Zend\Mail进阶:在ZF2的邮件中使用模板、多个附件以及用DI整合
- 根据两个百度地图坐标计算两点的距离
- 使用ZF2的DI操作Zend\Mail发送邮件
- The method setPositiveButton(int, DialogInterface.OnClickListener) in the type AlertDialog.Builder i
- ZF2多级树形路由Route配置实例
- c# chart保存为图片时背景为黑
- 在ZF2中实现Zend Framework的Controller init/preDispatch/postDispatch方法
- Xcode5.0 修改工程名
- 【美妙的Python之二】Python初步
- 语音识别系统原理介绍-----dnn-hmm
- HDU 4502 吉哥系列故事——临时工计划(DP)
- 05_android入门_GET方式实现登陆(在控件上显示服务端返回的内容)
- linux内核中断机制