Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Api Platform Pagination json format

i need help with the api platform pagination in json format.

here is my api_platform.yaml

api_platform:
    allow_plain_identifiers: true
    mapping:
        paths: ['%kernel.project_dir%/src/Entity']
    formats:
        json:     ['application/json']

when i used the format hydra (default) i got something like this

"hydra:view": {
    "@id": "/api/galleries?page=1",
    "@type": "hydra:PartialCollectionView",
    "hydra:first": "/api/galleries?page=1",
    "hydra:last": "/api/galleries?page=6",
    "hydra:next": "/api/galleries?page=2"
  }

can anyone help me? if it's possible to get something like that in json format or another way to aply pagination with api platform or symfony

thank you

like image 823
YeisonM Avatar asked Aug 07 '26 19:08

YeisonM


2 Answers

You shoud create an event Subscriber :

<?php

namespace App\EventSubscriber;

use ApiPlatform\Core\EventListener\EventPriorities;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Paginator;

final class PaginateJsonSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return [
            KernelEvents::VIEW => ['normalizePagination', EventPriorities::PRE_RESPOND],
        ];
    }

    public function normalizePagination(
        ViewEvent $event
    ): void {
        $method = $event->getRequest()->getMethod();

        if ($method !== Request::METHOD_GET) {
            return;
        }

        if (($data = $event->getRequest()->attributes->get('data')) && $data instanceof Paginator) {
            $json = json_decode($event->getControllerResult(), true);
            $pagination = [
                'first' => 1,
                'current' => $data->getCurrentPage(),
                'last' => $data->getLastPage(),
                'previous' => $data->getCurrentPage() - 1 <= 0 ? 1 : $data->getCurrentPage() - 1,
                'next' => $data->getCurrentPage() + 1 > $data->getLastPage() ? $data->getLastPage() : $data->getCurrentPage() + 1,
                'totalItems' => $data->getTotalItems(),
                'parPage' => count($data)
            ];
            $res = [
                "data" => $json,
                "pagination" => $pagination
            ];
            $event->setControllerResult(json_encode($res));
        }
    }
}
like image 65
wassim Boukadida Avatar answered Aug 10 '26 11:08

wassim Boukadida


In API Platform we have the jsonapi format which provides us the support for pagination. You can set the default config for number of records per page in api platform as:

# api/config/packages/api_platform.yaml
api_platform:
    defaults:
        pagination_items_per_page: 30 # Default value
    formats:
        jsonapi:  ['application/vnd.api+json']

With this configuration you would require to either have an item and collection normalizer to customise the structure of the response with pagination metadata OR you can use state providers for this purpose (check here)

You would still get all the necessary pagination metadata if you just leave your default format as jsonapi

This is the very good resource to read more about API Platform pagination

like image 37
Ehtisham Arshad Avatar answered Aug 10 '26 11:08

Ehtisham Arshad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!