gpt4 book ai didi

Cakephp - 使用 JSON API 进行分页

转载 作者:行者123 更新时间:2023-12-04 05:33:39 25 4
gpt4 key购买 nike

我正在(与合作伙伴)开发一个将使用 Backbone.js 的 CakePHP 应用程序。

所以,基本上,我的蛋糕应用程序在很大程度上就像一个 JSON API。在 Controller 操作加载某个 View (还没有呈现数据)之后,Backbone 对另一个 Controller 操作进行 AJAX 调用以获取数据。 Controller 返回 JSON。

(routes.php 文件有 mapResources 和 parseExtensions 行)

Controller 代码

public function index(){            
$company_id = $this->Session->read('Company.id');
$channel_ids = $this->Order->Channel->find('all', array('conditions' => array('Channel.company_id' => $company_id), 'fields' => array('Channel.id')));
$channel_ids = Set::extract('/Channel/id', $channel_ids);

$data = $this->paginate('Order', array('Order.fulfillment_status_id' => 1, 'Order.channel_id' => $channel_ids));
$this->set('orders', $data);
//In case of a JSON request
if($this->RequestHandler->ext == 'json') {
$this->autoRender = false;
echo json_encode($data);
}
}

这工作得很好,但是当我们试图用它实现分页时,我们遇到了麻烦。
  • 我们如何生成分页链接? (如果我们通过 Backbone 来做,它不知道数据库中的总记录数,因此不知道要生成多少链接)
  • “页码”应该如何烧入AJAX调用?它应该如何在 Controller 中解密?

  • 真的卡在这里,我们将非常感谢您的帮助。

    最佳答案

    在您的 Controller 中

    $this->layout = false;
    $this->RequestHandler->respondAs('json');
    $recipes = $this->paginate('Recipe');
    $this->set(compact('recipes'));

    在你看来
    echo json_encode($recipes);
    echo json_encode($this->Paginator->params());

    关于Cakephp - 使用 JSON API 进行分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12260994/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com