Installation Add the bundle via Composer:
composer require anh/pager-bundle
Register the bundle in config/bundles.php (Symfony 4+):
Anh\PagerBundle\AnhPagerBundle::class => ['all' => true],
Basic Usage
Inject the Pager service into your controller:
use Anh\PagerBundle\Pager\Pager;
class ProductController extends AbstractController
{
public function index(Pager $pager)
{
$products = $this->getDoctrine()
->getRepository(Product::class)
->findAll();
$pagination = $pager->paginate($products, $this->get('request')->query->get('page', 1), 10);
return $this->render('product/index.html.twig', [
'products' => $pagination->getItems(),
'pagination' => $pagination,
]);
}
}
First Use Case Display paginated results in a Twig template:
{% for product in products %}
{{ product.name }}
{% endfor %}
{{ pagination|pager_bundle_pagination() }}
Dynamic Pagination Pass request parameters directly:
$page = $request->query->get('page', 1);
$limit = $request->query->get('limit', 10);
$pagination = $pager->paginate($items, $page, $limit);
Customizing Pagination Views
Override Twig templates in templates/AnhPagerBundle/pagination/.
Example: Extend pagination.html.twig to add custom buttons.
Integration with Doctrine
Use paginateQuery() for database queries:
$query = $entityManager->createQuery('SELECT p FROM App\Entity\Product p');
$pagination = $pager->paginateQuery($query, $page, $limit);
API Responses Return pagination metadata as JSON:
return $this->json([
'data' => $pagination->getItems(),
'meta' => [
'current_page' => $pagination->getCurrentPage(),
'total_pages' => $pagination->getTotalPages(),
],
]);
total_items in cache if queries are expensive.config/packages/anh_pager.yaml:
anh_pager:
default_items_per_page: 15
default_max_per_page: 50
pager_bundle_ajax_pagination Twig filter for seamless updates.Deprecated Dependency
The bundle depends on sp/bower-bundle (v0.9), which is outdated. Ensure compatibility with your frontend setup.
Missing Documentation No official docs exist; rely on:
src/Resources/views/ for Twig templates.PagerInterface for method signatures.Query Pagination Quirks
paginateQuery() assumes Doctrine queries. For raw SQL, use paginate() with fetched results.
Twig Filter Conflicts
The pager_bundle_pagination filter may clash with other bundles. Prefix templates if needed:
{% include 'AnhPagerBundle::pagination/custom.html.twig' %}
dump($pagination->getIterator()->count());
$this->logger->debug('Pagination params:', [
'page' => $page,
'limit' => $limit,
'total' => count($items),
]);
Custom Pagination Classes
Implement Anh\PagerBundle\Pager\PaginationInterface for custom logic:
class CustomPagination implements PaginationInterface
{
public function getItems() { ... }
// ... other methods
}
Override Templates
Copy templates/AnhPagerBundle/pagination/ to your project and modify:
pagination.html.twig (main layout).slots/prev.html.twig (custom prev/next buttons).Add CSS/JS
Include assets via sp/bower-bundle or manually link:
{{ encore_entry_link_tags('app') }}
<link rel="stylesheet" href="{{ asset('bundles/anhpager/css/pager.css') }}">
How can I help you explore Laravel packages today?