2013-03-21 38 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