Php ZF3:如何根据方法和路由路由到特定控制器/操作?
在我的模块的Php ZF3:如何根据方法和路由路由到特定控制器/操作?,php,zend-framework,zend-route,zend-framework3,zend-router,Php,Zend Framework,Zend Route,Zend Framework3,Zend Router,在我的模块的module.config.php中,我有如下内容: namespace Application; return [ //... // myroute1 will route to IndexController fooAction if the route is matching '/index/foo' but regardless of request method 'myroute1' => [ 'type' => Zen
module.config.php
中,我有如下内容:
namespace Application;
return [
//...
// myroute1 will route to IndexController fooAction if the route is matching '/index/foo' but regardless of request method
'myroute1' => [
'type' => Zend\Router\Http\Literal::class,
'options' => [
'route' => '/index/foo',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'foo',
],
],
],
// myroute2 will route to IndexController fooAction if the route is request method is GET but regardless of requested route
'myroute2' => [
'type' => Zend\Router\Http\Method::class,
'options' => [
'verb' => 'get',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'foo',
],
],
],
//...
];
return [
//...
'myroute1' => [// The parent route will match the route "/index/foo"
'type' => Zend\Router\Http\Literal::class,
'options' => [
'route' => '/index/foo',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'foo',
],
],
'may_terminate' => false,
'child_routes' => [
'myroute1get' => [// This child route will match GET request
'type' => Method::class,
'options' => [
'verb' => 'get',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'foo'
],
],
],
'myroute1post' => [// This child route will match POST request
'type' => Method::class,
'options' => [
'verb' => 'post',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'bar'
],
],
]
],
],
//...
];
我正在努力实现的目标:
- 如果路由/index/foo被请求,并且是由GET方法请求的,那么它应该被路由到IndexController fooAction
- 如果请求路由/index/foo,并且是通过POST方法请求的,则应将其路由到IndexController栏操作(请注意,此处是barAction,而不是fooAction)
如何实现这一点?尝试将文本更改为
Zend\Mvc\Router\Http\Part
路由,然后将Http路由作为子路由放入
请参见此处尝试将文本更改为
Zend\Mvc\Router\Http\Part
路由,然后将Http路由作为子路由放入
请参见此处给我自己和其他任何人的一条说明,作为@delboy1978uk答案的补充说明 我想要的答案是这样的:
namespace Application;
return [
//...
// myroute1 will route to IndexController fooAction if the route is matching '/index/foo' but regardless of request method
'myroute1' => [
'type' => Zend\Router\Http\Literal::class,
'options' => [
'route' => '/index/foo',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'foo',
],
],
],
// myroute2 will route to IndexController fooAction if the route is request method is GET but regardless of requested route
'myroute2' => [
'type' => Zend\Router\Http\Method::class,
'options' => [
'verb' => 'get',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'foo',
],
],
],
//...
];
return [
//...
'myroute1' => [// The parent route will match the route "/index/foo"
'type' => Zend\Router\Http\Literal::class,
'options' => [
'route' => '/index/foo',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'foo',
],
],
'may_terminate' => false,
'child_routes' => [
'myroute1get' => [// This child route will match GET request
'type' => Method::class,
'options' => [
'verb' => 'get',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'foo'
],
],
],
'myroute1post' => [// This child route will match POST request
'type' => Method::class,
'options' => [
'verb' => 'post',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'bar'
],
],
]
],
],
//...
];
- 获取
=>IndexController fooAction/index/foo
- POST
=>IndexController命令/index/foo
module.config.php
文件中的代码可以如下所示:
namespace Application;
return [
//...
// myroute1 will route to IndexController fooAction if the route is matching '/index/foo' but regardless of request method
'myroute1' => [
'type' => Zend\Router\Http\Literal::class,
'options' => [
'route' => '/index/foo',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'foo',
],
],
],
// myroute2 will route to IndexController fooAction if the route is request method is GET but regardless of requested route
'myroute2' => [
'type' => Zend\Router\Http\Method::class,
'options' => [
'verb' => 'get',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'foo',
],
],
],
//...
];
return [
//...
'myroute1' => [// The parent route will match the route "/index/foo"
'type' => Zend\Router\Http\Literal::class,
'options' => [
'route' => '/index/foo',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'foo',
],
],
'may_terminate' => false,
'child_routes' => [
'myroute1get' => [// This child route will match GET request
'type' => Method::class,
'options' => [
'verb' => 'get',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'foo'
],
],
],
'myroute1post' => [// This child route will match POST request
'type' => Method::class,
'options' => [
'verb' => 'post',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'bar'
],
],
]
],
],
//...
];
给我自己和其他任何人的一封信,作为@delboy1978uk答案的补充 我想要的答案是这样的:
namespace Application;
return [
//...
// myroute1 will route to IndexController fooAction if the route is matching '/index/foo' but regardless of request method
'myroute1' => [
'type' => Zend\Router\Http\Literal::class,
'options' => [
'route' => '/index/foo',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'foo',
],
],
],
// myroute2 will route to IndexController fooAction if the route is request method is GET but regardless of requested route
'myroute2' => [
'type' => Zend\Router\Http\Method::class,
'options' => [
'verb' => 'get',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'foo',
],
],
],
//...
];
return [
//...
'myroute1' => [// The parent route will match the route "/index/foo"
'type' => Zend\Router\Http\Literal::class,
'options' => [
'route' => '/index/foo',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'foo',
],
],
'may_terminate' => false,
'child_routes' => [
'myroute1get' => [// This child route will match GET request
'type' => Method::class,
'options' => [
'verb' => 'get',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'foo'
],
],
],
'myroute1post' => [// This child route will match POST request
'type' => Method::class,
'options' => [
'verb' => 'post',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'bar'
],
],
]
],
],
//...
];
- 获取
=>IndexController fooAction/index/foo
- POST
=>IndexController命令/index/foo
module.config.php
文件中的代码可以如下所示:
namespace Application;
return [
//...
// myroute1 will route to IndexController fooAction if the route is matching '/index/foo' but regardless of request method
'myroute1' => [
'type' => Zend\Router\Http\Literal::class,
'options' => [
'route' => '/index/foo',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'foo',
],
],
],
// myroute2 will route to IndexController fooAction if the route is request method is GET but regardless of requested route
'myroute2' => [
'type' => Zend\Router\Http\Method::class,
'options' => [
'verb' => 'get',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'foo',
],
],
],
//...
];
return [
//...
'myroute1' => [// The parent route will match the route "/index/foo"
'type' => Zend\Router\Http\Literal::class,
'options' => [
'route' => '/index/foo',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'foo',
],
],
'may_terminate' => false,
'child_routes' => [
'myroute1get' => [// This child route will match GET request
'type' => Method::class,
'options' => [
'verb' => 'get',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'foo'
],
],
],
'myroute1post' => [// This child route will match POST request
'type' => Method::class,
'options' => [
'verb' => 'post',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'bar'
],
],
]
],
],
//...
];
这就是我要找的。但我注意到上面提到了“部分路线不打算直接使用”,这就是我想要的。但我注意到上面提到了“
部分
路线不是直接使用的”。你只是帮我节省了时间。敬礼我们还能像$this->url(“myroute1”、[“action”=>“foo”])
和$this->url(“myroute1”、[“action”=>“bar”])
那样调用吗?或者我们必须指定子路由name@AliRasheed我不确定哪一个可能有效,甚至可能两者都有效,但无论如何,这并不重要,因为它们都应该具有相同的URL,区别只是应用程序如何通过方法路由到操作used@AliRasheed顺便说一下,您不需要传递第二个参数,所以$this->url('myroute1')
就足够了,你刚刚帮我节省了时间。敬礼我们还能像$this->url(“myroute1”、[“action”=>“foo”])
和$this->url(“myroute1”、[“action”=>“bar”])
那样调用吗?或者我们必须指定子路由name@AliRasheed我不确定哪一个可能有效,甚至可能两者都有效,但无论如何,这并不重要,因为它们都应该具有相同的URL,区别只是应用程序如何通过方法路由到操作used@AliRasheed顺便说一下,您不需要传递第二个参数,所以$this->url('myroute1')
就足够了