因此,我试图发出安静的Web请求,但是,我可能配置了错误的内容,似乎无法解决。
我知道。
/companies GET -> index method
/companies POST -> add post method
/companies/1 GET -> show method
/companies/1 POST -> edit post method
这是我尝试过的
"router" => [
"routes" => [
"companies" => [
"type" => "segment",
"options" => [
"route" => "/companies[/:id]",
"constraints" => [
"id" => "[0-9]*",
],
"defaults" => [
"controller" => Controller\CompaniesController::class,
"action" => "index",
],
],
"may_terminate" => true,
"child_routes" => [
"companiesIndex" => [
"type" => "segment",
"options" => [
"verb" => "GET",
"route" => "/companies",
"defaults" => [
"controller" => Controller\CompaniesController::class,
"action" => "index"
],
],
],
"companiesAddPost" => [
"type" => "segment",
"options" => [
"verb" => "POST",
"route" => "/companies",
"defaults" => [
"controller" => Controller\CompaniesController::class,
"action" => "add"
],
],
],
"companiesShow" => [
"type" => "segment",
"options" => [
"verb" => "GET",
"route" => "/companies/:id",
"constraints" => [
"id" => "[0-9]*",
],
"defaults" => [
"controller" => Controller\CompaniesController::class,
"action" => "show"
],
],
],
"companiesEditPost" => [
"type" => "segment",
"options" => [
"verb" => "PATCH",
"route" => "/companies/:id",
"constraints" => [
"id" => "[0-9]*",
],
"defaults" => [
"controller" => Controller\CompaniesController::class,
"action" => "edit"
],
],
],
],
],
/companies
索引方法有效。不确定Post。但是,每当我尝试请求/companies/1
时,它仍然显示索引方法。有什么问题,我该如何解决。
答案 0 :(得分:1)
您有双重声明的路线。您已声明路线/companies[/:id]
并为其指定了child_routes:/companies
。本质上,您拥有:/companies
,/companies/:id
和/companies/companies
。另外,您正在使用segment
条路线。对于休息路线,您应该使用Method routes。
例如:
<?php
namespace Company;
use Company\Controller\Company\AddController;
use Company\Controller\Company\DeleteController;
use Company\Controller\Company\EditController;
use Company\Controller\Company\IndexController;
use Company\Controller\Company\ViewController;
use Zend\Router\Http\Method;
return [
'router' => [
'routes' => [
'companies_index' => [
'type' => Method::class,
'may_terminate' => true,
'options' => [
'verb' => 'GET',
'route' => '/companies',
'defaults' => [
'controller' => IndexController::class,
'action' => 'index',
],
],
'child_routes' => [
'companies_view' => [
'type' => Method::class,
'may_terminate' => true,
'options' => [
'verb' => 'GET',
'route' => '/:id',
'defaults' => [
'controller' => ViewController::class,
'action' => 'view',
],
],
],
'companies_edit' => [
'type' => Method::class,
'may_terminate' => true,
'options' => [
'verb' => 'PATCH',
'route' => '/:id',
'defaults' => [
'controller' => EditController::class,
'action' => 'edit',
],
],
],
'companies_delete' => [
'type' => Method::class,
'may_terminate' => true,
'options' => [
'verb' => 'DELETE',
'route' => '/:id',
'defaults' => [
'controller' => DeleteController::class,
'action' => 'delete',
],
],
],
],
],
'companies_add' => [
'type' => Method::class,
'may_terminate' => true,
'options' => [
'verb' => 'POST',
'route' => '/companies',
'defaults' => [
'controller' => AddController::class,
'action' => 'add',
],
],
],
],
],
];
也是这个子问题:
但是每当我尝试请求/ companies / 1时,它仍然会显示索引方法。
那是因为您的初始索引路由为/companies[/:id]
。因此,如果将/1
添加到请求的URL,该路由仍将匹配,并将您发送到您在此处配置的index
的{{1}}操作中。