MANCHUCK
MANCHUCK

Reputation: 2472

Zend Tree Route with multiple segments

I am trying to create a route with a child segment Example: /account/:accountId/user/edit/:userId

Module.config.php:

'router' => array(
    'routes' => array(
         'account' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/account/:accountid',
                'constraints' => array(
                    'accountid' => '[a-z0-9_-]*',
                ),
                'defaults' => array(
                    'controller' => 'My\Controller\Account',
                    'action'     => 'index',
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'user' => array(
                        'type' => 'segment',
                        'options' => array(
                            'route' => '/user/edit/:userid',
                            'constraints' => array(
                                'userid' => '[a-z0-9_-]*',
                            ),
                            'defaults' => array(
                                'action' => 'edit'
                            )
                        ),
                    )
                )
            ),
        ),

When I call:

<?= $this->url('account/user', ['accountid' => 'foo', 'userid' => 'bar']);

I only get: /account/foo where I want /account/foo/user/edit/bar

I did try to change may_terminate to false with no change

Upvotes: 0

Views: 224

Answers (1)

Alex Pogodin
Alex Pogodin

Reputation: 148

The same error I made myself several times and spent tremendous amount of time to resolve it.

Please, look carefully at your config. may_terminate & child_routes should be not inside options key, but on the same level as options. Correct config should look

'router' => array(
    'routes' => array(
         'account' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/account/:accountid',
                'constraints' => array(
                    'accountid' => '[a-z0-9_-]*',
                ),
                'defaults' => array(
                    'controller' => 'My\Controller\Account',
                    'action'     => 'index',
                ),
            ), // options
            'may_terminate' => true,
            'child_routes' => array(
                'user' => array(
                    'type' => 'segment',
                    'options' => array(
                        'route' => '/user/edit/:userid',
                        'constraints' => array(
                            'userid' => '[a-z0-9_-]*',
                        ),
                        'defaults' => array(
                            'action' => 'edit'
                        ),
                    ),
                ),
            ),
        ),
    ),
)

Upvotes: 2

Related Questions