Zend\Mvc\Router\Http\方法和子路由 [英] Zend\Mvc\Router\Http\Method and child routes
问题描述
我定义了两条路线,/ shoppingcart /和一条子路线/ shoppingcart / add /,仅应用于POST请求。
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',
),
),
),
)
),
)
/ shoppingcart /路线正常。子路线/ shoppingcart / add /不起作用(POST和GET出现404错误)。
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.
如何在子路由中使用Zend\Mvc\Router\Http\Method?
How can i use Zend\Mvc\Router\Http\Method in the child route?
推荐答案
您需要为子路径设置 may_terminate
true。
You need to set may_terminate
true for your child route.
此外,您还提到GET路由失败,如果您仅将动词设置为 post
,它将导致也想允许 get
,则动词应为 get,post
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
编辑:经过一些试验,结果证明我的理解是错误的,需要将 Method
类型放置为其保护的路由的父级。
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',
),
),
),
),
),
),
),
),
这篇关于Zend\Mvc\Router\Http\方法和子路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!