PHPackages                             ashleydawson/simple-pagination-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. ashleydawson/simple-pagination-bundle

ActiveSymfony-bundle[Utility &amp; Helpers](/categories/utility)

ashleydawson/simple-pagination-bundle
=====================================

Symfony bundle for Simple Pagination, the simple, lightweight and universal paginator that implements pagination on collections of things

5.0.0(6y ago)416.5k↑130.6%2[1 issues](https://github.com/AshleyDawson/SimplePaginationBundle/issues)MITPHPPHP ^7.2.5CI failing

Since Jul 15Pushed 6y ago1 watchersCompare

[ Source](https://github.com/AshleyDawson/SimplePaginationBundle)[ Packagist](https://packagist.org/packages/ashleydawson/simple-pagination-bundle)[ RSS](/packages/ashleydawson-simple-pagination-bundle/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (8)Dependencies (7)Versions (10)Used By (0)

Simple Pagination Bundle
========================

[](#simple-pagination-bundle)

[![Build Status](https://camo.githubusercontent.com/da45431cfa74f670aff2c25f8401269c504c293fe585c26241646b0cca5ab5e9/68747470733a2f2f7472617669732d63692e6f72672f4173686c6579446177736f6e2f53696d706c65506167696e6174696f6e42756e646c652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/AshleyDawson/SimplePaginationBundle)

Many thanks to [BrowserStack](https://www.browserstack.com/) for supporting open source projects like this one.

Symfony bundle for the [Simple Pagination](https://github.com/AshleyDawson/SimplePagination) library.

Installation
------------

[](#installation)

You can install the Simple Pagination Bundle via [Composer](https://getcomposer.org/). To do that, simply `require` the package:

```
$ composer req ashleydawson/simple-pagination-bundle
```

Run `composer update` to install the package. Then you'll need to register the bundle in your `app/AppKernel.php`:

```
$bundles = array(
    // ...
    new AshleyDawson\SimplePaginationBundle\AshleyDawsonSimplePaginationBundle(),
);
```

Basic Usage
-----------

[](#basic-usage)

The simplest collection we can use the paginator service on is an array. Please see below for an extremely simple example of the paginator operating on an array. This shows the service paginating over an array of 12 items.

```
namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class WelcomeController extends Controller
{
    public function indexAction()
    {
        // Get the paginator service from the container
        $paginator = $this->get('ashley_dawson_simple_pagination.paginator');

        // Build a mock set of items to paginate over
        $items = array(
            'Banana',
            'Apple',
            'Cherry',
            'Lemon',
            'Pear',
            'Watermelon',
            'Orange',
            'Grapefruit',
            'Blackcurrant',
            'Dingleberry',
            'Snosberry',
            'Tomato',
        );

        // Set the item total callback, simply returning the total number of items
        $paginator->setItemTotalCallback(function () use ($items) {
            return count($items);
        });

        // Add the slice callback, simply slicing the items array using $offset and $length
        $paginator->setSliceCallback(function ($offset, $length) use ($items) {
            return array_slice($items, $offset, $length);
        });

        // Perform the pagination, passing the current page number from the request
        $pagination = $paginator->paginate((int)$this->get('request')->query->get('page', 1));

        // Pass the pagination object to the view for rendering
        return $this->render('AcmeDemoBundle:Welcome:index.html.twig', array(
            'pagination' => $pagination,
        ));
    }
}
```

And in the twig view, it looks like this:

```
...

{# Iterate over items for the current page, rendering each one #}

    {% for item in pagination.items %}
        {{ item }}
    {% endfor %}

{# Iterate over the page list, rendering the page links #}

    {% for page in pagination.pages %}
        {{ page }} |
    {% endfor %}

...
```

You can override the "items per page" and "pages in range" options at runtime by passing values to the paginator like this:

```
// ...

$paginator
    ->setItemsPerPage(20)
    ->setPagesInRange(5)
;

$pagination = $paginator->paginate((int)$this->get('request')->query->get('page', 1));

// ...
```

Please note that this is **a very simple example**, some advanced use-cases and interfaces are coming up (see below).

Doctrine Example
----------------

[](#doctrine-example)

I've expanded the example above to use [Doctrine](http://www.doctrine-project.org/) instead of a static array of items:

```
namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class WelcomeController extends Controller
{
    public function indexAction()
    {
        // Get the paginator service from the container
        $paginator = $this->get('ashley_dawson_simple_pagination.paginator');

        // Create a Doctrine query builder
        $manager = $this->getDoctrine()->getManager();
        $query = $manager->createQueryBuilder();

        // Build the initial query, including any special filters
        $query
            ->from('AcmeDemoBundle:Film', 'f')
            ->where('f.releaseAt > :threshold')
            ->setParameter('threshold', new \DateTime('1980-08-16'))
        ;

        // Pass the item total callback
        $paginator->setItemTotalCallback(function () use ($query) {

            // Run the count of all records
            $query
                ->select('COUNT(f.id)')
            ;

            // Return the total item count
            return (int)$query->getQuery()->getSingleScalarResult();
        });

        // Pass the slice callback
        $paginator->setSliceCallback(function ($offset, $length) use ($query) {

            // Select and slice the data
            $query
                ->select('f')
                ->setFirstResult($offset)
                ->setMaxResults($length)
            ;

            // Return the collection
            return $query->getQuery()->getResult();
        });

        // Finally, paginate using the current page number
        $pagination = $paginator->paginate((int)$this->get('request')->query->get('page', 1));

        // Pass the pagination object to the view
        return $this->render('AcmeDemoBundle:Welcome:index.html.twig', array(
            'pagination' => $pagination,
        ));
    }
}
```

And in the twig view, it looks like this:

```
...

{# Iterate over films (Doctrine results) for the current page, rendering each one #}

    {% for film in pagination.items %}

            {{ film.title }}
            {{ film.releaseAt | date('jS F, Y') }}

    {% endfor %}

{# Use the pagination view helper to render the page navigation #}

    {{ simple_pagination_render(
        pagination,
        '_welcome',
        'page',
        app.request.query.all
    ) }}

...
```

Configuration
-------------

[](#configuration)

You can configure the Simple Pager Bundle from `app/config/config.yml` with the following **optional** parameters:

```
ashley_dawson_simple_pagination:
    defaults:
        items_per_page: 10
        pages_in_range: 5
        template: AshleyDawsonSimplePaginationBundle:Pagination:default.html.twig
```

Twig Function
-------------

[](#twig-function)

I've provided a handy twig function to render the built in pagination template. The default template can be configured in your `app/config/config.yml` or simply overridden as an argument in the twig function.

The arguments passed to the twig function are as follows:

```
simple_pagination_render(pagination : Pagination, routeName : string, [pageParameterName : string = 'page'], [queryParameters : array = array()], [template : string | null = null])

```

A brief description of each argument is:

- pagination: The `AshleyDawson\SimplePagination\Pagination` object returned by `AshleyDawson\SimplePagination\Paginator::paginate()`
- routeName: Route name to be passed to the navigation `{{ path() }}` function, defined in your routing config
- pageParameterName: The name of the page number parameter in your request (optional)
- queryParameters: The array of query parameters to append to the path (optional)
- template: The template you'd like to use to override the default (optional)

An exhaustive twig view example is as follows:

```

    {{ simple_pagination_render(
        pagination,
        '_welcome',
        'page',
        app.request.query.all,
        'AcmeBundle:Default:pagination.html.twig'
    ) }}

```

A better example, with the accompanying item list:

```

        {% for item in pagination.items %}
            {{ item }}
        {% endfor %}

    {{ simple_pagination_render(pagination, 'my_route_name', 'page', app.request.query.all) }}

```

Custom Service
--------------

[](#custom-service)

If you'd like to define the paginator as a custom service, please use the following service container configuration.

In YAML:

```
services:

  my_paginator:
    class: AshleyDawson\SimplePagination\Paginator
    calls:
      - [ setItemsPerPage, [ 10 ] ]
      - [ setPagesInRange, [ 5 ] ]
```

or in XML:

```

            10

            5

```

Then use it in your controllers like this:

```
// Get my paginator service from the container
$paginator = $this->get('my_paginator');

// ...
```

###  Health Score

34

—

LowBetter than 75% of packages

Maintenance15

Infrequent updates — may be unmaintained

Popularity31

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 83% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~307 days

Recently: every ~321 days

Total

8

Last Release

2212d ago

Major Versions

1.1.0 → 2.0.02019-06-24

2.0.0 → 3.0.02019-07-04

3.0.0 → 5.0.02020-06-04

PHP version history (2 changes)1.0.0PHP &gt;=5.3.3

5.0.0PHP ^7.2.5

### Community

Maintainers

![](https://www.gravatar.com/avatar/958ec06d53fff9021d93c8728740d74bf348a3ad22a4fb22ef6c1de9ee7f4f7a?d=identicon)[AshleyDawson](/maintainers/AshleyDawson)

---

Top Contributors

[![AshleyDawson](https://avatars.githubusercontent.com/u/1968942?v=4)](https://github.com/AshleyDawson "AshleyDawson (44 commits)")[![j0k3r](https://avatars.githubusercontent.com/u/62333?v=4)](https://github.com/j0k3r "j0k3r (5 commits)")[![jwilkinson87](https://avatars.githubusercontent.com/u/5273004?v=4)](https://github.com/jwilkinson87 "jwilkinson87 (3 commits)")[![AdamHornik](https://avatars.githubusercontent.com/u/2123073?v=4)](https://github.com/AdamHornik "AdamHornik (1 commits)")

---

Tags

symfonybundlepagerpaginatorpaginationSimplelightweightFlexibleeasyliteuniversal

### Embed Badge

![Health badge](/badges/ashleydawson-simple-pagination-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/ashleydawson-simple-pagination-bundle/health.svg)](https://phpackages.com/packages/ashleydawson-simple-pagination-bundle)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
