如何使用Apigility从Resource类访问URI查询参数?

时间:2015-02-16 17:04:11

标签: zend-framework2 http-request-parameters apigility

我正在开发基于ApigilityZend Framework 2驱动的应用程序。

我希望我的应用程序为单个项目和列表提供嵌套响应:

/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 conform解决方案(在这种情况下,项目包含每个项目列表项的图像列表,请参阅here)。我必须处理PaginatorDbAdapter并手动提供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
)

如何访问请求参数?

1 个答案:

答案 0 :(得分:1)

首先将请求参数添加到白名单中。它可以通过Apigility GUI完成,也可以直接在config:

中完成

<强> module.config.php

return array(
    ...
    'zf-rest' => array(
        ...
        'Portfolio\\V2\\Rest\\Project\\Controller' => array(
            ...
            'collection_query_whitelist' => array('page'),
            ...
        ),
        ...
    ),
);

然后可以通过Resource类的端点方法的参数访问该参数:

public function fetchAll($params = array()) {
    $projectService = $this->getProjectService();
    $config = $this->getServiceManager()->get('Config');
    $itemCountPerPage = $config['zf-rest']['Portfolio\\V2\\Rest\\Project\\Controller']['page_size'];
    $pageNumber = isset($params['page']) && intval($params['page']) > 0
        ? $params['page']
        : 1
    ;
    $offset = ($pageNumber - 1) * $itemCountPerPage;
    $projects = $projectService->getProjects($offset, $itemCountPerPage);
    return $projects;
}

另请参阅Apiglity文档:ZF REST -> Configuration -> User Configuration -> collection_query_whitelist