CakePHP 3.0无模型分页

时间:2015-09-03 18:05:08

标签: cakephp-3.0

我有来自API的数据,我希望显示使用CakePHP分页的结果。我为分页看到的所有东西都需要传入一个查询或模型对象。是否可以传递类似数据或总项目数,当前项目和每页项目的数据?

1 个答案:

答案 0 :(得分:1)

解决方案比我预期的要容易。我能够在请求参数中添加一个分页元素。分页组件根本不需要触及。

$data = $Utility->getData();
$perPage = 20;
$count = count($data);
$page = isset($this->request->query['page']) ? $this->request->query['page'] : 1;
$offset = $perPage * ($page - 1);
$pageCount = floor($count / $perPage);
$this->request->params['paging'] = [
    'Messages' => [
        'page' => $page,
        'current' => $offset,
        'count' => $count,
        'perPage' => $perPage,
        'prevPage' => $page != 1,
        'nextPage' => $page != $pageCount,
        'pageCount' => $pageCount,
    ]
];