前言
讀過一篇關于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
- ),
- ),
新聞熱點
疑難解答