0

我正在尝试使用子段创建路由示例:/account/:accountId/user/edit/:userId

模块.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'
                            )
                        ),
                    )
                )
            ),
        ),

当我打电话时:

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

我只得到: /account/foo 我想要的地方 /account/foo/user/edit/bar

我确实尝试将 may_terminate 更改为 false 而没有更改

4

1 回答 1

2

我自己犯了几次同样的错误,并花费了大量时间来解决它。

请仔细查看您的配置。may_terminate&child_routes不应在options键内,而应与options. 正确的配置应该看起来

'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'
                        ),
                    ),
                ),
            ),
        ),
    ),
)
于 2013-03-08T08:12:49.390 回答