ZF2动态路线

时间:2015-08-11 13:18:22

标签: php routing zend-framework2

我需要在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',
                        ),
                    ),
                ),
            ),
        ),
...

2 个答案:

答案 0 :(得分:1)

两种可能的方法:

  1. 使用regex route并在控制器中选择分开的网址。
  2. 通过实施RouteInterface创建您自己的路线。

答案 1 :(得分:0)

以下是使用正则表达式路线的解决方案:

...
                    '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-&gt; params() - &gt; fromRoute('dirPath');返回一个可以解析的url字符串。

相关问题