Zend Framework 2 Segment路由匹配'test'但不是'test /'

时间:2013-12-06 01:51:00

标签: php zend-framework routing zend-framework2 zend-framework-routing

我有两个模块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 /

我该如何解决?

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:0)

您可以添加仅与/匹配的可选子路由。这也适用于通配符(子)路径的相同问题。

'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' => '/')
        ),
    )
),
相关问题