Installation:
composer require white-october/pagerfanta-bundle
For Symfony 3.4/4.4/5.x, use the fork: composer require babdev/pagerfanta-bundle.
Enable the Bundle:
Add to config/bundles.php:
WhiteOctober\PagerfantaBundle\WhiteOctoberPagerfantaBundle::class => ['all' => true],
(Replace with BabDev\PagerfantaBundle\BabDevPagerfantaBundle for the fork.)
First Use Case:
Create a Pagerfanta instance in a controller and render it in Twig:
// src/Controller/ExampleController.php
use Pagerfanta\Pagerfanta;
use Pagerfanta\Adapter\ArrayAdapter;
public function indexAction()
{
$adapter = new ArrayAdapter([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
$pagerfanta = new Pagerfanta($adapter);
$pagerfanta->setMaxPerPage(3);
return $this->render('example/index.html.twig', [
'pagerfanta' => $pagerfanta,
]);
}
Render in Twig:
{% render_pagerfanta pagerfanta %}
This uses the default view. Customize later via configuration.
Adapter Integration:
Use Pagerfanta with any adapter (e.g., DoctrineORMAdapter for Doctrine entities):
use Pagerfanta\Adapter\DoctrineORMAdapter;
$adapter = new DoctrineORMAdapter(
$entityRepository->createQueryBuilder('e')->getQuery()
);
$pagerfanta = new Pagerfanta($adapter);
Twig Rendering:
{% render_pagerfanta pagerfanta %}{% render_pagerfanta pagerfanta with {'view': 'custom_view'} %}{% render_pagerfanta pagerfanta with {'view': 'default', 'options': {'max_per_page': 5}} %}Configuration:
Override defaults in config/packages/white_october_pagerfanta.yaml (or bab_dev_pagerfanta.yaml for the fork):
white_october_pagerfanta:
views:
default:
template: '@WhiteOctoberPagerfanta/default.html.twig'
options:
max_per_page: 10
default_outside_page_range: 2
Pagination in Forms:
Pass pagerfanta to Twig and use it in form actions:
<a href="{{ path('example_index', {'page': pagerfanta.getCurrentPage() + 1}) }}">
Next
</a>
Dynamic Views: Create reusable view configurations in Twig:
{% render_pagerfanta pagerfanta with {'view': 'bootstrap'} %}
Define bootstrap in config or use a service.
Service Integration:
Inject PagerfantaView service for dynamic rendering:
use WhiteOctober\PagerfantaBundle\Twig\PagerfantaExtension;
public function __construct(PagerfantaExtension $pagerfantaExtension) {
$this->pagerfantaExtension = $pagerfantaExtension;
}
public function renderPagerfanta($pagerfanta, $view = 'default', $options = []) {
return $this->pagerfantaExtension->render($pagerfanta, $view, $options);
}
API Responses: Serialize pagination metadata for APIs:
return $this->json([
'data' => $pagerfanta->getCurrentPageResults(),
'pagination' => [
'total_items' => $pagerfanta->getNbResults(),
'total_pages' => $pagerfanta->getNbPages(),
'current_page' => $pagerfanta->getCurrentPage(),
],
]);
Event Listeners:
Hook into PagerfantaEvents (e.g., View.Pagerfanta.Views.Load) to modify views dynamically.
Deprecation Warnings:
symfony2.0 branch.Twig Template Paths:
default.html.twig) exist in @WhiteOctoberPagerfanta/ or override them in your bundle.vendor/white-october/pagerfanta-bundle/Resources/views/.Adapter Quirks:
QueryBuilder is properly configured to return results for pagination.Configuration Overrides:
max_per_page vs. MaxPerPage).config/packages/ take precedence over bundle defaults.Caching Issues:
php bin/console cache:clear
Check Pagerfanta State: Dump the object to verify configuration:
dump($pagerfanta->getCurrentPage(), $pagerfanta->getNbPages());
Twig Debugging:
{% debug pagerfanta %} to inspect the object in Twig.render_pagerfanta function is available:
{% if pagerfanta is defined and pagerfanta|length > 0 %}
{% render_pagerfanta pagerfanta %}
{% endif %}
View Loading Errors:
TemplateNotFoundException, ensure:
default → default.html.twig).Performance:
setMaxPerPage() and lazy-loading adapters.DoctrineORMAdapter with setUseOutputWalkers).Custom Views: Extend the default view by creating a new template and configuring it:
# config/packages/white_october_pagerfanta.yaml
white_october_pagerfanta:
views:
custom:
template: '@YourBundle/Pagerfanta/custom.html.twig'
options:
previous_label: '« Previous'
next_label: 'Next »'
Dynamic Options: Pass options dynamically in Twig:
{% set options = {
'max_per_page': 5,
'default_outside_page_range': 3,
'view_parameters': {'class': 'pagination'}
} %}
{% render_pagerfanta pagerfanta with {'options': options} %}
Event Subscribers:
Listen to PagerfantaEvents to modify behavior:
use Pagerfanta\Event\View\ViewEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CustomPagerfantaSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
'view.pagerfanta.views.load' => 'onViewLoad',
];
}
public function onViewLoad(ViewEvent $event)
{
$view = $event->getView();
$view->template = '@YourBundle/Pagerfanta/custom.html.twig';
}
}
Integration with Forms:
Use Pagerfanta with Symfony Forms for paginated CRUD:
$form = $this->createForm(YourType::class, $pagerfanta->getCurrentPageResults());
$form->handleRequest($request);
Localization: Override labels in config:
white_october_pagerfanta:
views:
default:
options:
previous_label: 'Anterior'
next_label: 'Siguiente'
first_label: 'Primero'
last_label: 'Último'
How can I help you explore Laravel packages today?