0

I have an API like example

I have used cakephp HTTP client to get data, below my attempted code

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

I am trying to apply pagination with this country data then fetch it in view with pagination. After tried above code , I have gotten below error

Argument 1 passed to Cake\Datasource\Paginator::extractData() must be an instance of Cake\Datasource\RepositoryInterface, instance of Cake\Http\Client\Response given, called in \myapp\vendor\cakephp\cakephp\src\Datasource\Paginator.php on line 176

How can I get my desire result ?

2 Answers 2

1

You have probably need to implement a class who extend 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 ')); }

Is a bit tedious, because you need to define Json like a datasource.

Sign up to request clarification or add additional context in comments.

Comments

1

The default pagination only supports querying tables (repositories), or operating on pre-built query instances.

To extend on @Zeppi's answer. You basically have three somewhat straightforward options here:

  1. Create custom query/repository implementations as hinted by @Zeppi.

    This can indeed be quite a lot of work though, so you might want to look into alternatively implementing it with the help of plugins, for example muffin/webservice, which does most of the hard work of implementing the required interfaces.

  2. Or create a custom paginator that actually accepts and works on array data.

  3. Or use what is widely know as a "datatable", that is a JavaScript based table in the frontend that paginates the data, for example jQuery DataTables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.