【问题标题】:Zend\Mvc\Router\Http\Method and child routesZend\Mvc\Router\Http\Method 和子路由
【发布时间】:2013-03-21 17:32:23
【问题描述】:

我已经定义了两个路由,/shoppingcart/ 和一个子路由 /shoppingcart/add/,它们应该只对 POST 请求可用。

        'routes' => array(
        'shoppingcart' => array(
            'type'    => 'literal',
            'options' => array(
                'route'       => '/shoppingcart/',
                'defaults'    => array(
                    'controller' => 'ShoppingcartController',
                    'action'     => 'shoppingcart',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array (
                'add-product' => array(
                    'type' => 'method',
                    'options' => array(
                        'verb' => 'post',
                        'route' => 'add/',
                        'defaults' => array(
                            'controller' => 'ShoppingcartController',
                            'action' => 'addProductToShoppingcart',
                        ),
                    ),
                ),
            )
        ),
    )

路线 /shoppingcart/ 工作正常。子路由 /shoppingcart/add/ 不起作用(POST 和 GET 出现 404 错误)。

当我将类型从方法更改为文字并删除它工作的动词键时。

如何在子路由中使用 Zend\Mvc\Router\Http\Method ?

【问题讨论】:

    标签: php routes zend-framework2


    【解决方案1】:

    你需要为你的子路由设置may_terminatetrue。

    另外,你提到 GET 的路由失败,如果你只将动词设置为post,如果你也想允许get,动词应该是get,post

    编辑:经过一些实验,事实证明我的理解是错误的,Method 类型需要放置为它所保护的路由的父级......

    'routes' => array(
        'shoppingcart' => array(
            'type'    => 'literal',
            'options' => array(
                'route'       => '/shoppingcart/',
                'defaults'    => array(
                    'controller' => 'ShoppingcartController',
                    'action'     => 'shoppingcart',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array (
                'add-product' => array(
                    'type' => 'method',
                    'options' => array(
                        'verb' => 'get,post',
                    ),
                    'child_routes' => array(
                        // actual route is a child of the method
                        'form' => array(
                            'may_terminate' => true,
                            'type' => 'literal',
                            'options' => array(
                                'route' => 'add/',
                                'defaults' => array(
                                    'controller' => 'ShoppingcartController',
                                    'action' => 'addProductToShoppingcart',
                                ),
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
    

    【讨论】:

    • 我最初认为这会解决它,但我的类型设置为文字。它仍然具有与 type=literal 相同的行为,但不适用于 type=method。
    • @Crisp 你能解释一下如何在视图中使用这条路线吗? zf2中的方法路由我不是很了解。
    猜你喜欢
    • 2011-07-17
    • 2018-01-28
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    • 2014-05-02
    相关资源
    最近更新 更多