RESTAPI cakephp分页
我一直在研究cakephp。我创建了一个Api并配置了路由,但我想在json结果中显示分页,但我不能 这是我的密码:RESTAPI cakephp分页,php,cakephp,pagination,Php,Cakephp,Pagination,我一直在研究cakephp。我创建了一个Api并配置了路由,但我想在json结果中显示分页,但我不能 这是我的密码: class DronesController extends AppController { public $paginate = [ 'page' => 1, 'limit' => 5, 'maxLimit' => 6, 'fields' => [ 'id', 'user_id'
class DronesController extends AppController {
public $paginate = [
'page' => 1,
'limit' => 5,
'maxLimit' => 6,
'fields' => [
'id', 'user_id'
],
'sortWhitelist' => [
'id', 'user_id'
]
];
public function initialize()
{
parent::initialize();
$this->loadComponent('Paginator');
}
public function view($id)
{
$drone = $this->Drones->find()->where(['Drones.user_id' => $id], [
'contain' => ['Accounts', 'Cards', 'Pilotlogs']
]);
$this->set('drone', $this->paginate($drone));
$this->set('_serialize', ['drone']);
}
}
下面是我得到的结果:
我希望它是这样的:您在这里需要的分页详细信息在中可用,而不是分页组件 因此,我建议您在您的视图中修改这些数据
// View/Drones/json/view.ctp
$pagination['has_next_page'] = $this->Paginator->hasNext();
$pagination['has_prev_page'] = $this->Paginator->hasPrev();
$pagination['current_page'] = (int)$this->Paginator->current();
.....
.....
echo json_encode(compact("drone", "pagination"), JSON_PRETTY_PRINT);
如果您使用的是CakePHP 3,则可以在请求参数中检索Paginator组件生成的分页数据 在
AppController.php
中,将其置于序列化变量检查之前
if ($this->request->param('paging') !== false &&
in_array($this->response->type(), ['application/json', 'application/xml'])
) {
$this->set('paging', current($this->request->param('paging')));
}
该函数现在如下所示:
public function beforeRender(Event $event)
{
if ($this->request->param('paging') !== false &&
in_array($this->response->type(), ['application/json', 'application/xml'])
) {
$this->set('paging', current($this->request->param('paging')));
}
if (!array_key_exists('_serialize', $this->viewVars) &&
in_array($this->response->type(), ['application/json', 'application/xml'])
) {
$this->set('_serialize', true);
}
}
if
语句用于检查分页数据是否存在
$this->request->param('paging') // returns false if it does not exist
您可能会注意到current()
part。我用这个去掉了模特的名字
如果不使用current()
,结果将是:
"paging": {
"Posts": {
"finder": "all",
"page": 1,
"current": 6,
"count": 6,
"perPage": 10,
"prevPage": false,
"nextPage": false,
"pageCount": 1,
"sort": null,
"direction": false,
"limit": null,
"sortDefault": false,
"directionDefault": false
}
}
"paging": {
"finder": "all",
"page": 1,
"current": 6,
"count": 6,
"perPage": 10,
"prevPage": false,
"nextPage": false,
"pageCount": 1,
"sort": null,
"direction": false,
"limit": null,
"sortDefault": false,
"directionDefault": false
}
使用current()
的结果将是:
"paging": {
"Posts": {
"finder": "all",
"page": 1,
"current": 6,
"count": 6,
"perPage": 10,
"prevPage": false,
"nextPage": false,
"pageCount": 1,
"sort": null,
"direction": false,
"limit": null,
"sortDefault": false,
"directionDefault": false
}
}
"paging": {
"finder": "all",
"page": 1,
"current": 6,
"count": 6,
"perPage": 10,
"prevPage": false,
"nextPage": false,
"pageCount": 1,
"sort": null,
"direction": false,
"limit": null,
"sortDefault": false,
"directionDefault": false
}
CakePHP的PaginatorHelper
使用此方法访问分页数据
资料来源/参考:
干杯。使用
$this->request->param('paging')
不推荐使用
在CakePHP 3.7中,可以使用以下命令返回分页对象:
current($this->request->getParam('paging'))
在CakePHP4.0中,我们必须使用
$paging = $this->request->getAttribute('paging');
您希望您的结果是什么样的?首先请澄清一下。实际上我没有ctp,因为它是一个Api,所以它应该自动生成json。是的,你不需要ctp,除非你必须根据你的需求进行定制,就像在本例中一样。您不需要为其他Api模块创建ctp文件。我明白了。但是,即使我创建了ctp文件,也没有任何改变。你可以删除这行$this->set(“'u serialize',['drone']);从控制器重试。在视图中使用pr()或其他命令检查是否已执行。它可能无法执行视图代码。您还应该提到$components=array(..,'Requesthandler');你能告诉我你是怎么解决的吗?我有完全相同的问题,我无法在json结果中显示分页。