zend 框架 3 段路由不起作用

zend framework 3 segment routing doesn't work

我有以下 module.config.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' => Controller\LandingPageController::class,
                        'action' => 'index'
                    ]
                ],
                'may_terminate' => true,
            ]
        ]
    ],
    'controllers' => [
        'factories' => [
            Controller\LandingPageController::class => LandingPageControllerFactory::class
        ]
    ],
    'service_manager' => [
        'invokables' => [
            'LandingPage\Service\LandingPageService' => 'LandingPage\Service\LandingPageService'
        ]
    ]
];

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

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' => Controller\IndexController::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();
}

正在调用 /landingpage

Array
(
    [controller] => Application\Controller\IndexController
    [action] => index
)

呼叫/landingpage/show

Array
(
    [controller] => Application\Controller\IndexController
    [action] => show
)

调用/landingpage/show/1CGe2cveQ

Array
(
    [controller] => Application\Controller\IndexController
    [action] => show
    [id] => 1CGe2cveQ
)

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