PaginatorBundle
Simple paginator for Symfony2, which can paginate:
* array
* Doctrine\ORM\Query
* Doctrine\ORM\QueryBuilder
* Doctrine\ODM\MongoDB\Query\Builder
* Doctrine\ODM\MongoDB\Query\Query
* Doctrine\MongoDB\CursorInterface
* MongoCursor, (*1)
Installation
Add the following line to your composer.json require block, (*2)
"require": {
...
"arturdoruch/paginator-bundle": "~1.0"
}
and run composer command, (*3)
composer update arturdoruch/paginator-bundle
or simply, (*4)
composer require arturdoruch/paginator-bundle
Register ArturDoruchPaginatorBundle in your application kernel class, (*5)
// app/AppKernel.php
public function registerBundles()
{
return array(
// ...
new ArturDoruch\PaginatorBundle\ArturDoruchPaginatorBundle(),
);
}
Configuration
// app/config/config.yml
artur_doruch_paginator:
limit: 10 # Default value of displayed items per page
prev_page_label: '← Prev' # Pagination previous page button label
next_page_label: 'Next →' # Pagination next page button label
Usage
Controller
Get paginator in controller method., (*6)
$paginator = $this->get('arturdoruch_paginator');
Paginate items list., (*7)
$paginator->paginate($query, $page, $limit);
ArturDoruch\PaginatorBundle\Paginator::paginate() method receive three parameters:
* $query (mixed) A Doctrine ORM query or query builder, Doctrine mongodb ODM query or query builder,
instance of Doctrine\MongoDB\CursorInterface, instance of MongoCursor, or array with arrays of items.
* $page (integer) Number of page to display
* $limit (integer) The number of items per page. Possible values are:
* -1 - fetch all items (limit will be omitted)
* 0 - default limit (setting in config "artur_doruch_paginator.limit") will be used
* integer positive - given $limit value will be used, (*8)
Examples
Paginate items with Doctrine ORM query and query builder., (*9)
// AppBundle\Controller\ProjectController.php
public function listAction($page, Request $request)
{
$repository = $this->getDoctrine()->getRepository('AcmeProjectBundle:Project');
$paginator = $this->get('arturdoruch_paginator');
// Doctrine\ORM\QueryBuilder
$qb = $repository->createQueryBuilder('p')
->select('p');
$projects = $paginator->paginate($qb, $page, 5);
// Doctrine\ORM\Query
$query = $repository->createQueryBuilder('p')
->select('p')
->getQuery();
$projects = $paginator->paginate($query, $page, 5);
return $this->render('AppBundle:Project:list.html.twig', array(
'projects' => $projects
));
}
Paginate items with Doctrine ODM MongoDB query and query builder., (*10)
// todo
Paginate items with Doctrine\MongoDB\CursorInterface and MongoCursor., (*11)
// todo
Paginate items from array. Array can contain array or object collection., (*12)
// AppBundle\Controller\ProjectController.php
public function listAction($page, Request $request)
{
$projectsList = array(
array(
'id' => 1,
'name' => 'PHP'
),
array(
'id' => 2,
'name' => 'JS'
),
array(
'id' => 3,
'name' => 'Symfony'
),
array(
'id' => 4,
'name' => 'Github'
),
array(
'id' => 5,
'name' => 'SCSS'
)
...
);
$paginator = $this->get('arturdoruch_paginator');
$projects = $paginator->paginate($projectsList, $page, 5);
return $this->render('AppBundle:Project:list.html.twig', array(
'projects' => $projects
));
}
View
In twig template you can use several functions to display all paginate list data.
Each of them require Pagination class instance as parameter. See example below., (*13)
{# Pagination #}
{{ arturdoruch_pagination(projects.pagination) }}
{# Displayed items range #}
{{ arturdoruch_pagination_displayed_items(projects.pagination) }}
{# Total items count #}
{{ arturdoruch_pagination_total_items(projects.pagination) }}
{# Renders all pagination parts: pagination, items range, total items #}
{{ arturdoruch_pagination_all(projects.pagination) }}
Id |
Name |
{% for project in projects %}
{{ project.id }} |
{{ project.name }} |
{% endfor %}