简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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