我有一个类似于示例的API
我使用cakephp HTTP客户端获取数据,下面是我尝试的代码
public index()
{
$http = new Client();
$response = $http->get('https://restcountries.eu/rest/v2/all');
// $json = $response->getJson(); //also tried usgin json
$countries = $this->paginate($response);
$this->set(compact('countries '));
} 我正在尝试用这个国家的数据应用分页,然后从分页的角度来获取它。在尝试了以上代码之后,我得到了以下错误
参数1传递给Cake\Datasource\Paginator::extractData()必须是Cake\Datasource\RepositoryInterface的实例,Cake\Http\Client\Response的实例,在联机\myapp\vendor\cakephp\cakephp\src\Datasource\Paginator.php中调用
我怎样才能得到我渴望的结果呢?
发布于 2020-09-02 06:11:25
您可能需要实现一个扩展RepositoryInterface的类。
class JsonSource implements Cake\Datasource\RepositoryInterface
{ ... }
public index() {
$http = new Client();
$response = $http->get('https://restcountries.eu/rest/v2/all');
$src = new JsonSource();
$src->fromResponse($response);
$countries = $this->paginate($src);
$this->set(compact('countries ')); }这有点乏味,因为您需要像定义数据源一样定义Json。
发布于 2020-09-02 10:17:23
默认分页只支持查询表(存储库)或对预先构建的查询实例进行操作。
在@Zeppi的回答上扩展。基本上,这里有三个比较简单的选项:
https://stackoverflow.com/questions/63699494
复制相似问题