首页 > 解决方案 > 为什么 Zend 框架中的正则表达式路由不起作用?

问题描述

几个月前,我在使用 ZF3 的网站上工作,并且网站运行正常。现在我在不同的服务器上配置这个站点并且得到一些 url 的 404 错误。

配置代码是:

'router' => [
    'routes' => [
        'announcements' => [
            'type'    => Segment::class,
            'options' => [
                'route'    => '/announcements[/:id][/:action]',
                'constraints' => [
                    'id' => '[0-9]*',
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                ],
                'defaults' => [
                    'controller' => Controller\IndexController::class,
                    'action'     => 'index',
                ],
            ],                
        ],   
    ],      
],

http://example.com/announcements =>工作

http://example.com/announcements/2 =>工作

http://example.com/announcements/send =>不工作

那么有人可以建议我如何解决它吗?

谢谢

标签: phpzend-frameworkzend-framework2zend-framework3

解决方案


如果idURL 中的 完全是可选的,那么我建议实际上有三个路由:

'route'    => '/announcements[/:id][/:action]'
'route'    => '/announcements[/:id]'
'route'    => '/announcements[/:action]'

它们实际上是三个独立的端点,因为idaction可能同时出现、单独出现或根本不出现。


推荐阅读