phpzend-frameworkrouteszend-routezend-framework3

Child routes are not working


I'm new to Zend-Framework3.

And migrating my ZF2 application to ZF3.

In this child routes are not working.

Here is router from my module.config.php

'router' => [
    'routes' => [
        'application' => [
            'type' => Segment::class,
            'options' => [
                'route' => '/application',
                'defaults' => [
                    'controller' => Controller\IndexController::class,
                    'action' => 'index',
                ],
            ],
            'may_terminate' => true,
            'child_routes' => [
                'kk' => [
                    'type' => Literal::class,
                    'options' => [
                        'route' => 'kk',
                        'defaults' => [
                            'controller' => Controller\IndexController::class,
                            'action' => 'kk'
                        ],
                    ],
                ],
            ]
        ]
    ],
],

When I try to call /application/kk action. It generates 404 error.

Where am I wrong? Or do I have to register all actions manually?


Solution

  • ...do I have to register all actions manually?

    No, you are just missing / character in route value

    'router' => [
        'routes' => [
            'application' => [
                'type' => Segment::class,
                'options' => [
                    'route' => '/application',
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action' => 'index',
                    ],
                ],
                'may_terminate' => true,
                'child_routes' => [
                    'kk' => [
                        'type' => Literal::class,
                        'options' => [
                            'route' => '/kk', <-- here
                            'defaults' => [
                                'controller' => Controller\IndexController::class,
                                'action' => 'kk'
                            ],
                        ],
                    ],
                ]
            ]
        ],
    ],
    

    As long as action kk exists, you should not get 404 error.

    If your routes are same as actions name. You can use Segment type:

        'application' => [
            'type'    => Segment::class,
            'options' => [
                'route'    => '/application[/:action]',
                'constraints' => [
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
                ],
                'defaults' => [
                    'controller' => Controller\IndexController::class,
                    'action'     => 'index',
                ],
            ],
        ]