Search code examples
phprouteszend-framework2

Zf2 child route not working when it has one more child.


I am using zf2 and I have defined child routes like this.

'routes' => array(
            'test' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/test',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Test\Controller',
                        'controller'    => 'Test',
                        'action'        => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'testABC' => array(
                            'type' => 'Literal',
                            'options' => array(
                                    'route'    => '/abc',
                                    'defaults' => array(
                                            'action'     => 'abc',
                                    ),
                            ),
                            'child_routes' => array(
                                    'testABCDEF' => array(
                                            'type' => 'Segment',
                                            'options' => array(
                                                    'route'    => '/def/:id',
                                                    'defaults' => array(
                                                            'action'     => 'def',
                                                    ),
                                            ),
                                    ),
                                    'testABCXYZ' => array(
                                            'type' => 'Segment',
                                            'options' => array(
                                                    'route'    => '/xyz/:id',
                                                    'defaults' => array(
                                                            'action'     => 'xyz',
                                                    ),
                                            ),
                                    ),
                             ),
                    ),
                ),
            ),
        ),

In this only one route is not working, I don't know why?

  • localhost/test Working
  • localhost/test/abc Not working
  • localhost/test/abc/def/1 Working
  • localhost/test/abc/xyz/1 Working

Solution

  • The problem is caused because your testABC route is missing the may_terminate option.

    If it had no children, it would implicitly terminate, but since it has children, you must inform the router of the possibility explicitly (just like you did with its parent test route.)

    'testABC' => array(
        'type' => 'Literal',
        'options' => array(
            'route'    => '/abc',
            'defaults' => array(
                'action'     => 'abc',
            ),
        ),
        'may_terminate' => true, // inform the router
        'child_routes' => (
             // ...
        ),
    ),