我正在尝试创建一个包含子段的路由 示例:/ 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'
)
),
)
)
),
),
我打电话的时候:
<?= $this->url('account/user', ['accountid' => 'foo', 'userid' => 'bar']);
我只得到:/ account / foo我想要/ account / foo / user / edit / bar
我确实尝试将may_terminate更改为false而没有更改
答案 0 :(得分:2)
同样的错误我自己做了几次并花了很多时间来解决它。
请仔细查看您的配置。 may_terminate
&amp; 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'
),
),
),
),
),
),
)