如何在CakePHP 3.x中为子控制器配置路由?
问题描述:
-
Reports
有许多ReportInstances
-
ReportInstances
属于Reports
我想有网址/reports/:report-id/instances
指向动作index_by_report_id
内ReportInstancesController.php
如何在CakePHP 3.x中为子控制器配置路由?
如何相应地配置routes.php
?
UPDATE:
我想这里描述的嵌套资源: 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
你将能够做到以下几点:
- /父母/ 1 /儿童
- /parents/1/children.json
- /儿童
- /children.json
为什么这个工程?
http://book.cakephp.org/3.0/en/development/routing.html#creating-nested-resource-routes
告诉我们,基本上是基本检索来自请求参数父ID。
什么也没有明确说的是,该线路将然后再用基本的5个功能:指数,添加,查看,删除,父URL下编辑,甚至当你窝他们。
为什么你仍然有一个单独的儿童资源路径?
这允许/儿童和/children.json如果你需要他们以及工作。
什么补充的吗?
我还没有试过,但我不使用作为
- /父母/ 1 /儿童/加预见到任何问题
- /parents/1/children/add.json
- /儿童/加
- /children/add.json
'资源()'是REST风格的路线...你实际上是寻找REST风格的路线? – ndm
我想为RESTful和非RESTful网址嵌套路由。可能?? –
可能是(几乎)所有东西......但是,究竟如何设想这种工作方式,您需要以RESTful连接哪些URL,哪些不是? – ndm