dwalczyk/paginator-bundle

Symfony bundle for paginate

1.1.1 2024-01-31 20:37 UTC

This package is auto-updated.

Last update: 2024-05-30 00:36:35 UTC


README

Paginator Bundle for the Symfony Framework.

Installation

Composer

composer require dwalczyk/paginator-bundle

Add bundle to application

// config/bundles.php
DWalczyk\Paginator\PaginatorBundle::class => ['all' => true]

Usage

Base usage

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);
}

Custom data loader

  1. 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
    }
}
  1. Replace default data loader (doctrine QueryBuilder) with your new data loader.
# config/services.yaml
services:
  
  ...
  
  DWalczyk\Paginator\DataLoaderInterface: '@App\Service\SamplePaginatorDataLoader'
  1. done!