休息api cakephp分页

时间:2016-07-11 06:32:44

标签: php cakephp pagination

我一直在做cakephp。我创建了一个Api并配置了路由,但我想在json结果中显示分页,但我无法

这是我的代码:

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']);
  }
}

这里得到的结果是:http://imgur.com/ZEph0IB

我希望它是这样的:http://imgur.com/nUCQA4Q

4 个答案:

答案 0 :(得分:1)

如果您正在使用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()部分。我用它来剥离模型名称。

如果不使用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
    }
}

使用current()后,结果将是:

"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使用此方法访问分页数据。

来源/参考:Cake\View\Helper\PaginatorHelper

干杯。

答案 1 :(得分:0)

此处需要的分页详细信息可在Pagination Helper中找到,而不是分页组件。

因此,我建议您在视图中修改这些数据。

// 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);

答案 2 :(得分:0)

使用

$this->request->param('paging')

已弃用。

在CakePHP 3.7中,可以使用以下命令返回分页对象:

current($this->request->getParam('paging'))

答案 3 :(得分:0)

根据migration changes,在Cakephp 4.0中,我们必须使用

$paging = $this->request->getAttribute('paging');
相关问题