[英]ZF2 optional route constraints in child routes
I'm having an issue with an optional constraint in a route that is non-optional in it's children. 我在路径中的可选约束存在问题,该路径在其子项中是非可选的。 My routing structure is as follows:
我的路由结构如下:
'profile' => [
'type' => 'segment',
'options' => [
'route' => '/profile[/:id]',
'constraints' => ['id' => '[0-9]*'],
'defaults' => [
'controller' => 'User\Controller\User',
'action' => 'profile'
]
],
'may_terminate' => true,
'child_routes' => [
'sessions' => [
'type' => 'literal',
'options' => [
'route' => '/sessions',
'defaults' => ['action' => 'sessions']
]
]
]
]
Which to my mind should give me the following routes: 在我看来应该给我以下路线:
/profile
- works /profile
- 工作 /profile/123
- works /profile/123
- 有效 /profile/sessions
- doesn't work /profile/sessions
- 不起作用 /profile/123/sessions
- works /profile/123/sessions
- 工作 When I use route 3 in the URL view helper I get the following error: 当我在URL视图助手中使用路由3时,我收到以下错误:
$this->url('profile/sessions');
Zend\\Mvc\\Router\\Exception\\InvalidArgumentException
: Missing parameter "id"Zend\\Mvc\\Router\\Exception\\InvalidArgumentException
:缺少参数“id”
I originally had [0-9]+
as my constraint but making it optional ( *
) doesn't seem to have helped. 我最初有
[0-9]+
作为我的约束,但使它成为可选( *
)似乎没有帮助。 Has anyone experienced this case before? 以前有没有人遇到这个案子?
Add it to your parent route. 将其添加到父路线。
'profile' => [
'type' => 'segment',
'options' => [ // ↓
'route' => '/profile[/:id][/:action]',
'constraints' => [ 'id' => '[0-9]*', 'action' => '[a-zA-Z][a-zA-Z0-9_-]*' ],
'defaults' => [
'controller' => 'User\Controller\User',
'action' => 'profile',
],
],
]
This will make it optional to have id
and/or action
. 这将使其成为
id
和/或action
。 At least in theory it should make all your listed routes possible, there have been some issues with this. 至少在理论上它应该使所有列出的路线成为可能,这有一些问题 。
我曾经遇到同样的问题,我发现的唯一解决方案是创建一个单独的路由(在你的情况下为/ profile / sessions)作为基本路由的可选参数,在访问子路由时似乎成为必需的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.