ZF3 高级路由 - 使用 HTTP 方法
ZF3 advanced routing - With HTTP method
我正在使用 ZF3 创建一个站点。我在某些路线上遇到了麻烦。
示例,
我预计当我访问这个 URL : http://localhost/customer-import/ ,
如果 POST 方法:CustomerImportController::Process 将被执行,
如果 GET 方法:CustomerImportController::Index 将执行
实际:总是CustomerImportController::Index已经执行
配置文件:
'router' => [
'routes' => [
'customers' => [
'type' => Segment::class,
'options' => [
'route' => '/customers',
'defaults' => [
'controller' => Controller\CustomerController::class,
'action' => 'index',
],
],
],
'customers-import' => [
'type' => Literal::class,
'options' => [
'route' => '/customer-import-tool',
'defaults' => [
'controller' => Controller\CustomerImportController::class,
'action' => 'index',
],
],
'may_terminate' => true,
'child_routes' => [
'import_customer' => [
'type' => Method::class,
'options' => [
'verb' => 'post',
'defaults' => [
'controller' => Controller\CustomerImportController::class,
'action' => 'import',
],
],
],
],
],
],
],
我做错了什么?
你总是以 CustomerImportController::Index(-> customers-import
路线)结束,因为没有指定它必须只匹配 GET 请求。您正在点击相同的 url (host/customer-import-tool
),但您只声明了 POST 子路由。除了之前匹配 POST 和 GET。
这里的解决方法很简单:
- 你声明了主要的文字路线,但它没有调度员
- 您声明了两个方法子路径,一个用于 GET,一个用于 POST
'customers-import' => [
'type' => Literal::class,
'options' => [
// Here you specify the literal route
'route' => '/customer-import-tool',
'defaults' => [
'controller' => Controller\CustomerImportController::class
],
],
// Here you specify that "customer-import" can't be dispatched by itself,
// but only by its childs
'may_terminate' => false,
'child_routes' => [
// Here you match GET requests to the literal parent
'get_import_customer' => [
'type' => Method::class,
'options' => [
'verb' => 'get',
'defaults' => [
'controller' => Controller\CustomerImportController::class,
'action' => 'index'
]
]
],
// Here you match POST requests to the literal parent
'post_import_customer' => [
'type' => Method::class,
'options' => [
'verb' => 'post',
'defaults' => [
'controller' => Controller\CustomerImportController::class,
'action' => 'import'
]
]
]
]
],
我正在使用 ZF3 创建一个站点。我在某些路线上遇到了麻烦。
示例,
我预计当我访问这个 URL : http://localhost/customer-import/ , 如果 POST 方法:CustomerImportController::Process 将被执行, 如果 GET 方法:CustomerImportController::Index 将执行
实际:总是CustomerImportController::Index已经执行
配置文件:
'router' => [
'routes' => [
'customers' => [
'type' => Segment::class,
'options' => [
'route' => '/customers',
'defaults' => [
'controller' => Controller\CustomerController::class,
'action' => 'index',
],
],
],
'customers-import' => [
'type' => Literal::class,
'options' => [
'route' => '/customer-import-tool',
'defaults' => [
'controller' => Controller\CustomerImportController::class,
'action' => 'index',
],
],
'may_terminate' => true,
'child_routes' => [
'import_customer' => [
'type' => Method::class,
'options' => [
'verb' => 'post',
'defaults' => [
'controller' => Controller\CustomerImportController::class,
'action' => 'import',
],
],
],
],
],
],
],
我做错了什么?
你总是以 CustomerImportController::Index(-> customers-import
路线)结束,因为没有指定它必须只匹配 GET 请求。您正在点击相同的 url (host/customer-import-tool
),但您只声明了 POST 子路由。除了之前匹配 POST 和 GET。
这里的解决方法很简单: - 你声明了主要的文字路线,但它没有调度员 - 您声明了两个方法子路径,一个用于 GET,一个用于 POST
'customers-import' => [
'type' => Literal::class,
'options' => [
// Here you specify the literal route
'route' => '/customer-import-tool',
'defaults' => [
'controller' => Controller\CustomerImportController::class
],
],
// Here you specify that "customer-import" can't be dispatched by itself,
// but only by its childs
'may_terminate' => false,
'child_routes' => [
// Here you match GET requests to the literal parent
'get_import_customer' => [
'type' => Method::class,
'options' => [
'verb' => 'get',
'defaults' => [
'controller' => Controller\CustomerImportController::class,
'action' => 'index'
]
]
],
// Here you match POST requests to the literal parent
'post_import_customer' => [
'type' => Method::class,
'options' => [
'verb' => 'post',
'defaults' => [
'controller' => Controller\CustomerImportController::class,
'action' => 'import'
]
]
]
]
],