2013-03-21 39 views
1

我已经定义了两个路由,/ shoppingcart /和一个子路由/ shoppingcart/add /,它们只能用于POST请求。Zend Mvc Router Http 方法和子路由

 '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?

回答

3

您需要为您的子女路线设置may_terminate true。

另外,你提到的路线失败的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', 
          ), 
         ), 
        ), 
       ), 
      ), 
     ), 
    ), 
), 
+0

我首先认为这将修复它,但我喜欢的类型设置为文本。它仍然具有与type = literal类似的行为,但不具有type = method。 – Weteef 2013-03-22 09:52:09

+0

@ Weteef,对不起,请参阅编辑的版本 – Crisp 2013-03-22 17:09:03

+0

太棒了,它的工作原理!非常感谢! – Weteef 2013-03-23 10:52:01