Search code examples
zend-framework2zend-route

ZF2 Child Routing issues


I'm having some troubles setting up child_routes. They don't work unless I separate them, althou the end result should be the same!:

This is what I'm trying to achieve:

'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',
                                ),
                            ),
                        ),
                    ),
                ),
            ),
        ),

But it only works this way:

'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',
                    ),
                ),
            ),
        ),

.. anyone knows what I might be doing wrong..?


Solution

  • Your child routes are in the wrong place, they don't belong inside the options array, nor does the may_terminate key, try this...

    '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',
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),