ZF2子路由问题

时间:2013-03-06 15:01:05

标签: zend-framework2 zend-route

我遇到了设置child_routes的麻烦。 除非我把它们分开,否则它们不起作用,最终结果应该是相同的!:

这就是我想要实现的目标:

'router' => array(
        'routes' => array(
            'app' => array(
                'type' => 'Zend\Mvc\Router\Http\Segment',
                'options' => array(
                    'route' => '[/:info]/app',
                    'defaults' => array(
                        '__NAMESPACE__' => 'X\App',
                        'controller' => 'Index',
                        'action' => 'index',
                    ),
                    'may_terminate' => true,
                    'child_routes' => array(
                        'example' => array(
                            'type' => 'Zend\Mvc\Router\Http\Segment',
                            'options' => array(
                                'route' => '/example[:/data]',
                                'defaults' => array(
                                    'action' => 'example',
                                ),
                            ),
                        ),
                    ),
                ),
            ),
        ),

但它只能这样运作:

'router' => array(
        'routes' => array(
            'app' => array(
                'type' => 'Zend\Mvc\Router\Http\Segment',
                'options' => array(
                    'route' => '[/:info]/app',
                    'defaults' => array(
                        '__NAMESPACE__' => 'X\App',
                        'controller' => 'Index',
                        'action' => 'index',
                    ),                    
                ),
            ),
            'app.example' => array(
                'type' => 'Zend\Mvc\Router\Http\Segment',
                'options' => array(
                    'route' => '[/:info]/app/example[/:data]',
                    'defaults' => array(
                        '__NAMESPACE__' => 'X\App',
                        'controller' => 'Index',
                        'action' => 'example',
                    ),
                ),
            ),
        ),

..谁知道我可能做错了什么..?

2 个答案:

答案 0 :(得分:6)

您的子路线位于错误的位置,它们不属于options数组,may_terminate key也不属于此类...

'router' => array(
    'routes' => array(
        'app' => array(
            'type' => 'Zend\Mvc\Router\Http\Segment',
            'options' => array(
                'route' => '[/:info]/app',
                'defaults' => array(
                    '__NAMESPACE__' => 'X\App',
                    'controller' => 'Index',
                    'action' => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'example' => array(
                    'type' => 'Zend\Mvc\Router\Http\Segment',
                    'options' => array(
                        'route' => '/example[:/data]',
                        'defaults' => array(
                            'action' => 'example',
                        ),
                    ),
                ),
            ),
        ),
    ),
),

答案 1 :(得分:1)

您的语法错误

在第一个例子中,你的chil_routes定义在你的选择数组中,它必须与options数组在同一级别上:

'router' => array(
    'routes' => array(
        'app' => array(
            'type' => 'Zend\Mvc\Router\Http\Segment',
            'options' => array(
                'route' => '[/:info]/app',
                'defaults' => array(
                    '__NAMESPACE__' => 'X\App',
                    'controller' => 'Index',
                    'action' => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'example' => array(
                    'type' => 'Zend\Mvc\Router\Http\Segment',
                    'options' => array(
                        'route' => '/example[:/data]',
                        'defaults' => array(
                            'action' => 'example',
                        ),
                    ),
                ),
            ),
        ),
    ),