0

我正在开发基于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 符合解决方案(在这种情况下,项目具有每个项目列表项的图像列表,请参见此处)。我必须处理Paginatorand并手动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
)

如何访问请求参数?

4

1 回答 1

1

请求参数必须首先添加到白名单中。它可以通过 Apigility GUI 或直接在配置中完成:

模块.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;
}

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

于 2015-02-16T19:02:01.997 回答