【问题标题】:Rest api cakephp paginationRest api cakephp 分页
【发布时间】:2016-11-13 00:27:10
【问题描述】:

我一直在做 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

【问题讨论】:

  • 您希望您的结果看起来如何?请先澄清这一点。

标签: php cakephp pagination


【解决方案1】:

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

$paging = $this->request->getAttribute('paging');

【讨论】:

    【解决方案2】:

    使用

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

    已弃用。

    在 CakePHP 3.7 中可以使用以下方法返回分页对象:

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

    【讨论】:

      【解决方案3】:

      如果您使用的是 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

      干杯。

      【讨论】:

        【解决方案4】:

        您需要的分页详细信息在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);
        

        【讨论】:

        • 其实我没有ctp,因为它是一个Api,所以它应该自动生成json
        • 是的,你并不真的需要一个 ctp,除非你必须根据你的要求进行定制,就像在这种情况下一样。您无需为其他 Api 模块创建 ctp 文件。
        • 我明白了。但即使我创建了 ctp 文件,也没有任何改变
        • 你可以删除这一行 $this->set('_serialize', ['drone']);从控制器并重试。在视图中使用 pr() 或其他东西来检查它是否已被执行。它可能无法执行视图代码。您还应该提到 $components = array(...., 'Requesthandler');
        • 您能发布一下您是如何解决的吗?我有完全相同的问题,我无法在 json 结果中显示分页。
        猜你喜欢
        • 2012-07-03
        • 2016-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多