如何在 CakePHP 3.x 中为子控制器配置路由?
how to configure routes for child controllers in CakePHP 3.x?
Reports
有很多 ReportInstances
ReportInstances
属于 Reports
我想让 url /reports/:report-id/instances
指向 ReportInstancesController.php
中的操作 index_by_report_id
如何相应地配置 routes.php
?
更新:
我尝试了这里描述的嵌套资源:
http://book.cakephp.org/3.0/en/development/routing.html#creating-nested-resource-routes
这是我的路线
$routes->resources('Reports', [
'map' => [
'standard' => [
'action' => 'standard',
'method' => 'GET',
]
]
]);
$routes->resources('Reports', function ($routes) {
$routes->resources('ReportInstances');
});
当我执行 /reports/1/instances
时,它会转到 ReportsController 寻找操作 1。
请指教
在您的 routes.php
中执行此操作
$routes->resources('Parents', function ($routes) {
$routes->resources('Children');
});
$routes->resources('Children');
在ChildrenController.php
、
protected function _prepareConditions() {
$parentId = isset($this->request->params['parent_id']) ? $this->request->params['parent_id'] : null;
if ($parentId == null) {
return [];
}
return [
'Children.parent_id' => $parentId
];
}
public function index()
{
$conditions = $this->_prepareConditions();
$this->paginate = [
'contain' => ['Parents'],
'conditions' => $conditions
];
// ... and so on
您将能够执行以下操作:
- /parents/1/children
- /parents/1/children.json
- /children
- /children.json
为什么这样有效?
http://book.cakephp.org/3.0/en/development/routing.html#creating-nested-resource-routes
告诉我们基本上从请求参数中检索 parent id。
它没有明确说明的是,路由将重用基本的 5 个功能:索引、添加、查看、删除、编辑,即使您将它们嵌套在 parent url 下也是如此.
为什么Children还有单独的资源路由?
这允许 /children 和 /children.json 在您需要时也能工作。
添加呢?
我没试过,但我预计使用它不会有任何问题
- /parents/1/children/添加
- /parents/1/children/add.json
- /children/add
- /children/add.json
Reports
有很多ReportInstances
ReportInstances
属于Reports
我想让 url /reports/:report-id/instances
指向 ReportInstancesController.php
index_by_report_id
如何相应地配置 routes.php
?
更新:
我尝试了这里描述的嵌套资源: http://book.cakephp.org/3.0/en/development/routing.html#creating-nested-resource-routes
这是我的路线
$routes->resources('Reports', [
'map' => [
'standard' => [
'action' => 'standard',
'method' => 'GET',
]
]
]);
$routes->resources('Reports', function ($routes) {
$routes->resources('ReportInstances');
});
当我执行 /reports/1/instances
时,它会转到 ReportsController 寻找操作 1。
请指教
在您的 routes.php
$routes->resources('Parents', function ($routes) {
$routes->resources('Children');
});
$routes->resources('Children');
在ChildrenController.php
、
protected function _prepareConditions() {
$parentId = isset($this->request->params['parent_id']) ? $this->request->params['parent_id'] : null;
if ($parentId == null) {
return [];
}
return [
'Children.parent_id' => $parentId
];
}
public function index()
{
$conditions = $this->_prepareConditions();
$this->paginate = [
'contain' => ['Parents'],
'conditions' => $conditions
];
// ... and so on
您将能够执行以下操作:
- /parents/1/children
- /parents/1/children.json
- /children
- /children.json
为什么这样有效?
http://book.cakephp.org/3.0/en/development/routing.html#creating-nested-resource-routes
告诉我们基本上从请求参数中检索 parent id。
它没有明确说明的是,路由将重用基本的 5 个功能:索引、添加、查看、删除、编辑,即使您将它们嵌套在 parent url 下也是如此.
为什么Children还有单独的资源路由?
这允许 /children 和 /children.json 在您需要时也能工作。
添加呢?
我没试过,但我预计使用它不会有任何问题
- /parents/1/children/添加
- /parents/1/children/add.json
- /children/add
- /children/add.json