【问题标题】:Why this ZF2 router doesn't match this two child routes?为什么这个 ZF2 路由器不匹配这两个子路由?
【发布时间】:2014-07-12 15:34:45
【问题描述】:

这个路由器出了什么问题:

    '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 ?带有 404 的 zend 框架 2 响应 - 找不到页面 - “请求的 URL 无法通过路由匹配”。这两条路由在路由器定义中。

【问题讨论】:

  • 我发现了一个问题,'may_terminate' 应该在选项之外。
  • 请发帖回答
  • 我必须等待 10 小时才能回答我的问题。 10小时后应该会出现。不同之处在于 'api' 路线 - 'may_terminate' 应该在 'options' 数组之外,所以我们必须在 'may_terminate' 之前添加 ) 并在最后删除一个 )。

标签: routing zend-framework2


【解决方案1】:

正确的路由器应该是这样的:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    • 1970-01-01
    • 2011-12-07
    • 1970-01-01
    • 2014-07-06
    相关资源
    最近更新 更多