lucas-gerard/range-pagination

This package is abandoned and no longer maintained. No replacement package was suggested.

A Symfony 4 bundle to handle pagination with Range HTTP headers

v1.0.1 2018-04-27 09:37 UTC

This package is not auto-updated.

Last update: 2023-02-23 11:30:30 UTC


README

A Symfony 4 bundle to handle pagination with Range HTTP headers

Composer Install

Just run the following command to install the bundle :

composer require lucas-gerard/range-pagination

And register the EventSubscriber in services.yaml :

[...]
services:
    LucasGerard\RangePagination\EventSubscriber\:
        resource: '../vendor/lucas-gerard/range-pagination/EventSubscriber'

And you should be done !

Usage

Header Format

This bundle is basically providing your controllers a Range object by parsing a Range HTTP Header using these formats :

Range: items=0-9
// Or
Range: items=0-
// Or
Range: items=0-*

Access the Range instance

You can now access a Range object in your controllers, these are the two possibilities :

/**
 * Inject a range in your controller
 */
public function indexAction(Range $range)
{
    // You can use $range here
}

/**
 * Access it from your request object
 */
public function indexAction(Request $request)
{
    $range = $request->attributes->get('range');
    // Then use $range
}

Get the items

If you use the findAll() method, switch it for a findBy() and set the 3rd and 4th parameter :

$items = $repository->findAll()
// Switch it to this :
$items = $repository->findBy([], null, $range->getLimit(), $range->getStart());

If you access data using a Doctrine Query :

$qb = [...]; // A QueryBuilder
$qb->getQuery()
    ->setFirstResult($range->getStart())
    ->setMaxResults($range->getLimit());

$items = $qb->getResult(); // Returns a collection containing the items within the range

Build a response

Everything should be pretty straightforward, just set the content of your JsonResponse, the Response Code and Content-Range header are automatically handled.

Please

Check out the Range class to see all available methods.

Todo

  • Tests
  • Config options (enable/disable response codes & headers, etc..)

Licence

This bundle is under the MIT license. For the whole copyright, see the LICENSE file distributed with this source code.

Credits

Lucas Gerard : lucas.gerard.web@gmail.com

Feel free to send suggestions and/or pull requests !