bcrowe / cakephp-api-pagination

:bookmark_tabs: CakePHP 4 plugin that injects pagination information into API responses.
MIT License
40 stars 14 forks source link

API not getting response Data object #9

Closed abhi-meena closed 3 years ago

abhi-meena commented 3 years ago

Hi there, I am using this plugin for CakePHP 4 but I am not getting the data, its only giving pagination object. my function is as follows:- class ArticlesController extends AppController { /**

and its giving the output:- { "pagination": { "count": 2, "current": 2, "perPage": 20, "page": 1, "requestedPage": 1, "pageCount": 1, "start": 1, "end": 2, "prevPage": false, "nextPage": false, "sort": null, "direction": null, "sortDefault": false, "directionDefault": false, "completeSort": [], "limit": null, "scope": null, "finder": "all" } } if I remove $this->paginate() method and use $this->set('articles', $this->Articles->find("all")->toArray()); then output comes:- { "articles": [ { "id": 1, "user_id": 1, "title": "First Post", "slug": "first-post", "body": "This is the first post.", "published": true, "created": "2021-04-29T15:51:26+05:30", "modified": "2021-04-29T15:51:26+05:30" }, { "id": 2, "user_id": 2, "title": "Test Category", "slug": "testing", "body": "Hello Body", "published": false, "created": "2021-04-29T11:13:06+05:30", "modified": "2021-04-29T11:23:28+05:30" } ] } I have tried everything but articles data not coming only pagination is coming.

pabloelcolombiano commented 3 years ago

@abhi-meena The setting of serialized data in CakePHP4 slightly changed from CakePHP3: See here.

Does this solve your issue?

abhi-meena commented 3 years ago

@abhi-meena The setting of serialized data in CakePHP4 slightly changed from CakePHP3: See here.

Does this solve your issue?

@pabloelcolombiano Thanks for your help but it's not working, I have tried all these things but all these things are not working if I am using cakephp-api-pagination. If i remove $this->loadComponent('BryanCrowe/ApiPagination.ApiPagination'); this line from AppController.php then data comes but pagination get removed.

pabloelcolombiano commented 3 years ago

I suggest you copy paste your controller in a readable incremented manner, using the ApiPagination and the modification I suggested, in order to help me reproduce the issue.

abhi-meena commented 3 years ago

here are my files with composer.json Controller.zip

pabloelcolombiano commented 3 years ago

I will not open a zip file @abhi-meena, this is not secure. Did you load the plugin in Application.php, as mentioned here and documented on how to in CakePHP4 there?

abhi-meena commented 3 years ago

I will not open a zip file @abhi-meena, this is not secure. Did you load the plugin in Application.php, as mentioned here and documented on how to in CakePHP4 there?

Hey @pabloelcolombiano Thanks for your Help, I got the solution. public function index() { $articles = $this->paginate($this->Articles); $this->set('articles' ,$articles ); $this->viewBuilder()->setOption('serialize',['articles']); }

bcrowe commented 3 years ago

Thanks for helping out on this @pabloelcolombiano!