-
Notifications
You must be signed in to change notification settings - Fork 3
Description
Description
I am using a custom CollectionView configuration to modify the structure of the paginated JSON response. Below is the configuration file I am using:
<?php
return [
'CollectionView' => [
'collection' => '{{collection}}', // array that holds pagination data
'collection.url' => '{{url}}', // URL of the current page
'collection.count' => '{{count}}', // items on this page
'collection.page' => '{{page}}', // current page number
'collection.pages' => '{{pages}}', // total pages
'collection.total' => '{{total}}', // total records in the database
'collection.next' => '{{next}}', // next page URL
'collection.prev' => '{{prev}}', // previous page URL
'collection.first' => '{{first}}', // first page URL
'collection.last' => '{{last}}', // last page URL
'data' => '{{data}}', // the collection of data
]
];However, when I try to use this configuration, I get a response that still displays the placeholder {{page}} instead of the current page number:
{
"collection": {
"url": "/account-transactions?account_id=2&transactionDuration=all&page=1",
"count": 20,
"page": "{{page}}",
"pages": 26,
"total": 501,
"next": "/account-transactions?account_id=2&transactionDuration=all&page=2",
"prev": "",
"first": "/account-transactions?account_id=2&transactionDuration=all&page=1",
"last": "/account-transactions?account_id=2&transactionDuration=all&page=26"
}
}Steps to Reproduce
- Create a
config/collection_view.phpfile and use the custom configuration mentioned above. - Access a paginated endpoint in your application.
- Observe that the response includes the literal placeholder
{{page}}instead of the actual page number.
Expected Behavior
The collection.page key should be replaced by the current page number in the JSON response, such as:
json
{
"page": 1
}
Actual Behavior
The response displays the placeholder value {{page}} instead of the actual page number.
Potential Cause
It appears that the Serializer class does not correctly handle the custom collection.page key. Looking at the Serializer class, it seems that the collection() method does not properly replace the placeholder {{page}} with the correct value.
Additional Context
Here's the relevant snippet from the Serializer class:
php
if ($this->paginator instanceof PaginatorHelper) {
$return[$collection][$next] = $this->paginator->next();
$return[$collection][$prev] = $this->paginator->prev();
$return[$collection][$first] = $this->paginator->first();
$return[$collection][$last] = $this->paginator->last();
$return[$collection][$pages] = $this->paginator->total();
$return[$collection][$total] = intval($this->paginator->param('totalCount'));
$return[$collection]['page'] = $this->paginator->param('page'); // This line should replace {{page}}
}