Skip to content

[mixerapi/collection-view] Feature Request : Ability to display Current Page Number #158

@angelxmoreno

Description

@angelxmoreno

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&amp;transactionDuration=all&amp;page=2",
        "prev": "",
        "first": "/account-transactions?account_id=2&transactionDuration=all&page=1",
        "last": "/account-transactions?account_id=2&amp;transactionDuration=all&amp;page=26"
    }
}

Steps to Reproduce

  1. Create a config/collection_view.php file and use the custom configuration mentioned above.
  2. Access a paginated endpoint in your application.
  3. 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}}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions