Installation
composer require white-october/pagerfanta-bundle
Add to config/bundles.php (Symfony 4+):
return [
// ...
WhiteOctober\PagerfantaBundle\WhiteOctoberPagerfantaBundle::class => ['all' => true],
];
First Use Case: Basic Pagination in a Controller
use WhiteOctober\PagerfantaBundle\Pagerfanta\PagerfantaAdapter;
use Pagerfanta\Pagerfanta;
public function indexAction()
{
$data = $this->getDoctrine()->getRepository('App:Post')->findAll();
$adapter = new PagerfantaAdapter(new ArrayAdapter($data));
$pagerfanta = new Pagerfanta($adapter);
$pagerfanta->setMaxPerPage(10);
return $this->render('post/index.html.twig', [
'pagerfanta' => $pagerfanta,
]);
}
Render in Twig
{{ pagerfanta|pagerfanta_default('post/index') }}
Doctrine ORM Integration
Use DoctrineORMAdapter for seamless pagination with Doctrine repositories:
$query = $this->getDoctrine()->getRepository('App:Post')->createQueryBuilder('p')->getQuery();
$adapter = new DoctrineORMAdapter($query);
$pagerfanta = new Pagerfanta($adapter);
Custom Views
Extend default views by creating a new Twig template (e.g., views/pagerfanta/custom.html.twig) and pass it to the Twig filter:
{{ pagerfanta|pagerfanta_custom('post/index') }}
Reusing Options
Define reusable options in config/packages/white_october_pagerfanta.yaml:
white_october_pagerfanta:
views:
default:
options:
previous: '« Previous'
next: 'Next »'
Dynamic Max Per Page Allow users to select items per page:
$pagerfanta->setMaxPerPage($request->query->getInt('per_page', 10));
AJAX Pagination
Use pagerfanta_ajax Twig filter for AJAX-driven pagination:
{{ pagerfanta|pagerfanta_ajax('post/index', { 'per_page': 20 }) }}
Symfony Version Mismatch
white-october/pagerfanta-bundle only if you’re on Symfony 2.0+ (check composer.json constraints).knplabs/knp-paginator-bundle (modern alternative).Twig Filter Overrides
pagerfanta_default or pagerfanta_custom filters don’t work, ensure:
bundles.php.config/packages/twig.yaml for conflicts).Doctrine Query Caching
DoctrineORMAdapter may cache queries aggressively. Disable caching if needed:
$query->setMaxResults(10)->setFirstResult(($page-1)*10); // Manual pagination
CSRF in AJAX Requests
config/packages/security.yaml:
access_control:
- { path: ^/ajax/pagerfanta, roles: PUBLIC_ACCESS }
Check Pagerfanta Instance
Dump the Pagerfanta object to verify data:
dump($pagerfanta->getCurrentPageResults()->count());
Inspect Twig Variables
Use {{ dump(pagerfanta) }} in Twig to debug the passed object structure.
Clear Cache
After modifying white_october_pagerfanta.yaml, run:
php bin/console cache:clear
Custom Adapter
Extend PagerfantaAdapter for non-Doctrine data sources (e.g., Elasticsearch):
class ElasticsearchAdapter extends Pagerfanta\Adapter\AdapterInterface {
public function getItems($offset, $length) { /* ... */ }
public function getNbItems() { /* ... */ }
}
Override Twig Filters Create a custom Twig extension to modify pagination behavior:
// src/Twig/PagerfantaExtension.php
class PagerfantaExtension extends \Twig\Extension\AbstractExtension {
public function getFunctions() {
return [
new \Twig\TwigFunction('custom_pagerfanta', [$this, 'renderPagerfanta']),
];
}
}
Dynamic View Selection Pass dynamic view names based on user roles or contexts:
{% set view = app.user ? 'admin_pager' : 'default' %}
{{ pagerfanta|pagerfanta(view) }}
How can I help you explore Laravel packages today?