如何在CakePHP 3.x中为子控制器配置路由? [英] how to configure routes for child controllers in CakePHP 3.x?
问题描述
-
报告
有许多ReportInstances
-
ReportInstances
属于报告
$ b b
我想要有 / reports /:report-id / instances
指向操作 index_by_report_id
ReportInstancesController.php
如何配置
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 = [
'contains'=> ['Parents'],
'conditions'=> $ conditions
];
// ...等等
您将能够执行以下操作:
- / parents / 1 / children
- /parents/1/children.json
- / children
- /children.json
>为什么这样工作?
http://book.cakephp.org/3.0/en/development/routing.html#creating-nested-resource-routes
告诉我们基本上从请求参数中检索父标识符。
它没有明确说明的是,路由
为什么你仍然拥有这些功能?
这允许/ children和/children.json在需要的时候也可以工作。
添加?
我没有尝试过,但我没有预见到任何问题
- / parents / 1 / children / add
- / parents / 1 / children / add.json
- / children / add
- /children/add.json
Reports
has many ReportInstances
ReportInstances
belongs to Reports
I would like to have the url /reports/:report-id/instances
to point to the action index_by_report_id
inside ReportInstancesController.php
How do I configure the routes.php
accordingly?
UPDATE:
I tried nested resources as described here:
http://book.cakephp.org/3.0/en/development/routing.html#creating-nested-resource-routes
Here are my routes
$routes->resources('Reports', [
'map' => [
'standard' => [
'action' => 'standard',
'method' => 'GET',
]
]
]);
$routes->resources('Reports', function ($routes) {
$routes->resources('ReportInstances');
});
When I do a /reports/1/instances
, it goes to ReportsController looking for action 1.
Please advise.
解决方案 Do this in your routes.php
$routes->resources('Parents', function ($routes) {
$routes->resources('Children');
});
$routes->resources('Children');
In 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
You will be able to do the following:
- /parents/1/children
- /parents/1/children.json
- /children
- /children.json
Why this works?
http://book.cakephp.org/3.0/en/development/routing.html#creating-nested-resource-routes
tells us that basically to basically retrieve the parent id from the request params.
What it does not say explicitly is that, the routes will then reuse the basic 5 functions: index, add, view, delete, edit even when you nest them under a parent url.
Why do you still have a separate resources route for the Children?
This allows the /children and /children.json to work if you need them as well.
What about add?
I haven't tried that but I do not foresee any issues with using that as
- /parents/1/children/add
- /parents/1/children/add.json
- /children/add
- /children/add.json
这篇关于如何在CakePHP 3.x中为子控制器配置路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文