Zend 框架 3 段路由不起作用



我有以下模块.php:

return [
'router' => [
'routes' => [
'landingpage' => [
'type' => Segment::class,
'options' => [
'route' => '/landingpage[/:action/:id]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z0-9_-]*'
],
'defaults' => [
'controller' => ControllerLandingPageController::class,
'action' => 'index'
]
],
'may_terminate' => true,
]
]
],
'controllers' => [
'factories' => [
ControllerLandingPageController::class => LandingPageControllerFactory::class
]
],
'service_manager' => [
'invokables' => [
'LandingPageServiceLandingPageService' => 'LandingPageServiceLandingPageService'
]
]
];

我正在尝试使用以下路线,但它不起作用:

http://localhost:8081/landingpage/show/1CGe2cveQ

如果我使用以下路线,它可以工作:

http://localhost:8081/landingpage/show

如果我将以前的路由与/一起使用,则不起作用:

http://localhost:8081/landingpage/show/

如果您需要更多信息,请告诉我。 谢谢。

路由声明中有一个双斜杠:路由由/landingpage/匹配,后跟/:action/:id。如果删除此双斜杠,路由将按预期工作。

'route' => '/landingpage[/:action/:id]',

此外,我建议您修改路由声明以使 id 可选:

'route' => '/landingpage[/:action[/:id]]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z0-9_-]+'
]

测试:配置

'landingpage' => [
'type' => Segment::class,
'options' => [
'route' => '/landingpage[/:action[/:id]]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z0-9_-]*'
],
'defaults' => [
'controller' => ControllerIndexController::class,
'action' => 'index'
]
],
'may_terminate' => true,
],

索引控制器:

public function indexAction () {
print '<pre>' . print_r($this->params()->fromRoute(), true);
die(); 
}
public function showAction(){
print '<pre>' . print_r($this->params()->fromRoute(), true);
die();
}

调用/登陆页面

Array
(
[controller] => ApplicationControllerIndexController
[action] => index
)

调用/登陆页面/显示

Array
(
[controller] => ApplicationControllerIndexController
[action] => show
)

调用/landingpage/show/1CGe2cveQ

Array
(
[controller] => ApplicationControllerIndexController
[action] => show
[id] => 1CGe2cveQ
)

不要忘记清除配置缓存(如果它启用了;)

最新更新