Zend\Mvc\Router\Http\Method and child routes

泪湿孤枕 提交于 2019-12-13 17:21:19

问题


i have defined two routes, /shoppingcart/ and a child route /shoppingcart/add/ which should only be available for POST requests.

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

The route /shoppingcart/ works fine. The child route /shoppingcart/add/ does not work(404 error with POST and GET).

When i change the type from method to literal and remove the verb key it works.

How can i use Zend\Mvc\Router\Http\Method in the child route?


回答1:


You need to set may_terminate true for your child route.

Also, you mention route failing for GET, which it will if you only set the verb to post, if you want to allow get too, the verb should be get,post

Edit: after a little experimenting, it turns out my understanding was wrong, the Method type needs to be placed as parent of the route it's protecting....

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


来源:https://stackoverflow.com/questions/15554309/zend-mvc-router-http-method-and-child-routes

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