为什么这个ZF2路由器不匹配这两个子路由?

时间:2014-07-12 15:34:45

标签: routing zend-framework2

此路由器有什么问题:

    'routes' => array(
        'home' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    'controller' => 'Application\Controller\Index',
                    'action'     => 'index',
                ),
            ),
        ),
        'application' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/application',
                'defaults' => array(
                    '__NAMESPACE__' => 'Application\Controller',
                    'controller'    => 'Index',
                    'action'        => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'default' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route'    => '/[:controller[/:action]]',
                        'constraints' => array(
                            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                        ),
                    ),
                ),
            ),
        ),
        'api'         => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/api',
                'defaults' => array(
                    '__NAMESPACE__' => 'Application\Controller',
                    'controller'    => 'Api',
                    'action'        => 'index',                 
                ),
                'may_terminate' => true,
                'child_routes'  => array(
                    'post' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route'    => '/post/:id',
                            'constraints' => array(
                                    'id'     => '[0-9]+',
                            ),
                            'defaults' => array(
                                    'controller' => 'Application\Controller\Api',
                                    'action'     => 'post',
                            ),
                        ),
                    ),  
                    'page' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route'    => '/page/:name',
                            'constraints' => array(
                                    'name'     => '[a-zA-Z]+',
                            ),
                            'defaults' => array(
                                    'controller' => 'Application\Controller\Api',
                                    'action'     => 'page',
                            ),
                        ),
                    ),
                ),
            ),
        ),  
    ),

我无法路由到http://example.com/api/post/0http://example.com/api/page/pagename? zend框架2响应404 - 页面未找到 - “请求的URL无法通过路由匹配”。路由器定义中有两条路由。

1 个答案:

答案 0 :(得分:0)

正确的路由器应如下所示:

'routes' => array(
    'home' => array(
        'type' => 'Zend\Mvc\Router\Http\Literal',
        'options' => array(
            'route'    => '/',
            'defaults' => array(
                'controller' => 'Application\Controller\Index',
                'action'     => 'index',
            ),
        ),
    ),
    'application' => array(
        'type'    => 'Literal',
        'options' => array(
            'route'    => '/application',
            'defaults' => array(
                '__NAMESPACE__' => 'Application\Controller',
                'controller'    => 'Index',
                'action'        => 'index',
            ),
        ),
        'may_terminate' => true,
        'child_routes' => array(
            'default' => array(
                'type'    => 'Segment',
                'options' => array(
                    'route'    => '/[:controller[/:action]]',
                    'constraints' => array(
                        'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                    ),
                    'defaults' => array(
                    ),
                ),
            ),
        ),
    ),
    'api'         => array(
        'type'    => 'Literal',
        'options' => array(
            'route'    => '/api',
            'defaults' => array(
                '__NAMESPACE__' => 'Application\Controller',
                'controller'    => 'Api',
                'action'        => 'index',                 
            ),
        ),
        'may_terminate' => true,
        'child_routes'  => array(
            'post' => array(
                'type'    => 'Segment',
                'options' => array(
                    'route'    => '/post/:id',
                    'constraints' => array(
                            'id'     => '[0-9]+',
                    ),
                    'defaults' => array(
                            'controller' => 'Application\Controller\Api',
                            'action'     => 'post',
                    ),
                ),
            ),  
            'page' => array(
                'type'    => 'Segment',
                'options' => array(
                    'route'    => '/page/:name',
                    'constraints' => array(
                            'name'     => '[a-zA-Z]+',
                    ),
                    'defaults' => array(
                            'controller' => 'Application\Controller\Api',
                            'action'     => 'page',
                    ),
                ),
            ),
        ),
    ),
),