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

老子叫甜甜 提交于 2019-12-24 19:27:08

问题


I have two modules Admin and Application. In the module application I have the following route in my 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'
            )
    )
),

The problem is that it is matching

example.com/admin

and is not matching

example.com/admin/

How do I fix that?


回答1:


Insert [/] to fix it. try:

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



回答2:


You can add an optional child-route which matches only a /. That should also work for the same problem with a wildcard (sub)route.

'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' => '/')
        ),
    )
),


来源:https://stackoverflow.com/questions/20414479/zend-framework-2-segment-route-matching-test-but-not-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!