繁体   English   中英

ZF2动态路线

[英]ZF2 dynamic routes

我需要在zf2中构建动态多级路由,但不确定采用哪种方法。 我需要的是:

/ listing [/:directory1 [/:directory2 [/ dir ...]]作为无限嵌套来模仿目录结构。

理想情况下,所有目录都应以数组形式出现,或者至少应使用单个变量,例如“ directory1 / directory2 / directory3 ...”。

我找不到任何可以正确设置这些参数的位置,即使zf2支持这种路由也是如此。 我当前的路由配置如下所示:

...
        'gallery' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/Gallery',
                'defaults' => array(
                    '__NAMESPACE__' => 'Application\Controller',
                    'controller'    => 'Gallery',
                    'action'        => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'listing' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route'    => '/listing[/:directory1[/:directory2]]',
                        'constraints' => array(
                            'directory1' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'directory2' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                            '__NAMESPACE__' => 'Application\Controller',
                            'controller'    => 'Listing',
                            'action'        => 'index',
                        ),
                    ),
                ),
            ),
        ),
...

两种可能的方法:

  1. 使用正则表达式路由,并在控制器中分开URL。
  2. 通过实现RouteInterface创建自己的路由。

这是使用正则表达式路由的解决方案:

...
                    'listing' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route'    => '/listing',
                        'constraints' => array(
                            'user' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                            '__NAMESPACE__' => 'Application\Controller',
                            'controller'    => 'Listing',
                            'action'        => 'index',
                        ),
                    ),
                    'may_terminate' => true,
                    'child_routes' => array(
                        'directory' => array(
                            'type' =>'regex',
                            'options' => array(
                                'regex' => '/(?<dirPath>.*)',
                                'defaults' => array(
                                    '__NAMESPACE__' => 'Application\Controller',
                                    'controller' => 'Listing',
                                    'action' => 'index',
                                ),
                                'spec' => '/%dirPath%',
                            ),


                        ),
...

内部控制器$ directory = $ this-> params()-> fromRoute('dirPath'); 返回可以解析的网址字符串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM