我正在开发基于Zend Framework 2的Apigility驱动应用程序。
我希望我的应用程序同时为单个项目和列表提供嵌套响应:
/projects/1
{
"id": "1",
"title": "...",
...
"_embedded": {
"images": [
{
"id": "1",
"project_id": "1",
"title": "...",
...
},
{
"id": "2",
"project_id": "1",
"title": "...",
...
}
]
},
...
}
/projects
{
...
"_embedded": {
"projects": [
{
"id": "1",
"title": "...",
...
"_embedded": {
"images": [
{
"id": "1",
"project_id": "1",
"title": "...",
...
},
...
]
},
...
},
...
]
},
"total_items": 2
}
由于我还没有找到用于实现具有嵌套列表的列表的 apigility 符合解决方案(在这种情况下,项目具有每个项目列表项的图像列表,请参见此处)。我必须处理Paginator
and并手动DbAdapter
提供参数:page
class ProjectResource extends AbstractResourceListener {
...
public function fetchAll($params = array()) {
$pageNumber = $this->getEvent()->getRouteMatch()->getParam('page', 1); <-- that doesn't work
$projectService = $this->getProjectService();
$offset = $pageNumber > 0 ? $pageNumber - 1 : 0;
$config = $this->getServiceManager()->get('Config');
$itemCountPerPage = $config['zf-rest']['Portfolio\\V2\\Rest\\Project\\Controller']['page_size'];
$projects = $projectService->getProjects($offset, $itemCountPerPage);
return $projects;
}
...
}
问题是,这$this->getEvent()->getRouteMatch()->getParam('page', 1)
行不通。代替page
参数,$this->getEvent()->getRouteMatch()->getParams()
返回
Array
(
[controller] => Portfolio\V2\Rest\Project\Controller
[version] => 2
)
如何访问请求参数?