繁体   English   中英

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

[英]Why this ZF2 router doesn't match this two child routes?

此路由器有什么问题:

    '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 Framework 2的404错误-找不到页面-“路由无法匹配所请求的URL”。 这两个路由在路由器定义中。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM