Zend Framework 2分段路由匹配';测试';但不是';测试/';


Zend Framework 2 Segment Route matching 'test' but not 'test/'

我有两个模块Admin和Application。在模块应用程序中,我在module.config.php中有以下路径:

'admin' => array(
    'type' => 'Segment',
    'options' => array(
            'route' => '/admin[/:controller[/:action]]',
            'constraints' => array(
                    'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
            ),
            'defaults' => array (
                    '__NAMESPACE__' => 'Admin'Controller',
                    'module' => 'Admin',
                    'controller' => 'Index',
                    'action' => 'index',
            ),
    ),
    'may_terminate' => true,
    'child_routes' => array(
            'wildcard' => array(
                    'type' => 'Wildcard'
            )
    )
),

问题是它与匹配

example.com/admin

并且与不匹配

example.com/admin/

我该怎么解决?

插入[/]进行修复。尝试:

'route' => '/admin[/:controller[/:action]][/]',

您可以添加一个只匹配a/的可选子路由。这也适用于通配符(子)路由的相同问题。

'admin' => array(
    'type' => 'Segment',
    'options' => array(
        'route' => '/admin[/:controller[/:action]]',
        'constraints' => array(
                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
        ),
        'defaults' => array (
            '__NAMESPACE__' => 'Admin'Controller',
            'module' => 'Admin',
            'controller' => 'Index',
            'action' => 'index',
        ),
    ),
    'may_terminate' => true,
    'child_routes' => array(
        'wildcard' => array(
            'type' => 'Wildcard'
            'may_terminate' => true,
            'child_routes' => array(
                'ts' => array(
                    'type' => 'literal',
                    'options' => array('route' => '/' )
                ),
            )
        ),
        'ts' => array(
                'type' => 'literal',
                'options' => array('route' => '/')
        ),
    )
),