前言
读过一篇关于Zend Framework2的技术文章《ZF2多级树形路由Route配置实例》,是介绍路由配置的。我觉得很有意思,这是的需求:
/user对应用户列表页面
/user/:user_id对应用户的个人主页,比如 /user/AlloVince 就对应AlloVince用户的个人主页
/user/:user_id/blog/对应用户的博客列表页面,比如 /user/AlloVince/blog 就会列出AlloVince写过的Blog
/user/:user_id/blog/:blog_id对应用户的一篇博客文章
方案引用自原文:
- '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
- ),
- ),
新闻热点
疑难解答