Paginator Bundle for the Symfony Framework.
composer require dwalczyk/paginator-bundle// config/bundles.php
DWalczyk\Paginator\PaginatorBundle::class => ['all' => true]Doctrine QueryBuilder is supported by default.
use DWalczyk\Paginator\PaginatorInterface;
#[Route('/users')]
public function __invoke(PaginatorInterface $paginator)
{
$res = $paginator->paginate(
$target = $this->getEm()->createQueryBuilder()->select('u')->from(User::class, 'u'),
$page = 1,
$itemsPerPage = 30
);
dump($res);
}- Create class that implements DWalczyk\Paginator\DataLoaderInterface
<?php
namespace App\Service;
use DWalczyk\Paginator\DataLoaderInterface;
class SamplePaginatorDataLoader implements DataLoaderInterface
{
public function loadItems(mixed $target, int $offset, int $limit): array
{
// some logic here
}
public function loadTotalCount(mixed $target): int
{
// some logic here
}
}- Replace default data loader (doctrine QueryBuilder) with your new data loader.
# config/services.yaml
services:
...
DWalczyk\Paginator\DataLoaderInterface: '@App\Service\SamplePaginatorDataLoader'- done!