dev-master
9999999-devSymfony 2 paginator bundle.
The Requires
- php >=5.3.0
- symfony/symfony >=2.0
- beberlei/doctrineextensions 0.3.0
by Moisés Maciá
paginator recordset
Symfony 2 paginator bundle.
This package contains a bundle to easily paginate complex queries efficiently and without effort., (*1)
For the bundle to work properly you should also install DoctrineExtensions\Paginate
., (*2)
Bundle and instructions at https://github.com/beberlei/DoctrineExtensions, (*3)
Add IdeupSimplePaginatorBundle in your composer.json:, (*4)
"require": { "php": ">=5.3.3", "symfony/symfony": "2.1.*", . . . . . "ideup/simple-paginator-bundle":"*" }
Now download and install the bundle, (*5)
``` bash $ php composer.phar update ideup/simple-paginator-bundle, (*6)
Composer will install the bundle to your project's `vendor/ideup` directory. ## How to include SimplePaginatorBundle in your code You should clone this repository in your Symfony's `vendor/bundles` directory, add it into `autoload.php` file: ```php <?php $loader->registerNamespaces(array( 'Symfony' => array(__DIR__.'/../vendor/symfony/src', __DIR__.'/../vendor/bundles'), ... 'Ideup' => __DIR__.'/../vendor/bundles', );
... and in your AppKernel.php
file:, (*7)
<?php public function registerBundles() { $bundles = array( ... new Ideup\SimplePaginatorBundle\IdeupSimplePaginatorBundle(), ); }
... so you are ready now to use IdeupSimplePaginatorBundle as a service., (*8)
Since the Paginator::paginate()
method needs a Query
object to work with, you need to change slightly your entity Repository classes:, (*9)
<?php class User extends EntityRepository { public function findByMyCriteria() { $query = $this->_em->createQuery('...'); return $query->getResult(); } }
<?php class User extends EntityRepository { public function findByMyCriteria() { return $this->findByMyCriteriaDQL()->getResult(); } public function findByMyCriteriaDQL() { $query = $this->_em->createQuery('...'); return $query; } }
In your controller you can be able to instantiate the paginator service. SimplePaginatorBundle
is smart enough to
detect the current page and the maximum items per page from the Request
context, so you don't need to type more
boilerplate code!, (*10)
<?php class MyController extends Controller { public function listAction() { $paginator = $this->get('ideup.simple_paginator'); $users = $paginator->paginate($em->getRepository('MyBundle:User')->findByMyCriteriaDQL())->getResult(); $vars = array( 'users' => $users, 'paginator' => $paginator); return $this->render('MyBundle:User:list.html.twig', $vars); } }
Note that the variable $users
contains only the paginated subset of the Doctrine collection and you can query
$paginator
object to obtain information about the pagination process; such as how many items are in the full
collection, in wich page are we, wich is the last page, etc., (*11)
<?php class MyController extends Controller { public function listAction() { $paginator = $this->get('ideup.simple_paginator'); $users = $paginator->paginate($em->getRepository('MyBundle:User')->findByMyCriteriaDQL())->getResult(); $vars = array( 'users' => $users, ); return $this->render('MyBundle:User:list.html.twig', $vars); } }
Note that now you don't need to pass $paginator
variable to template unless you want to obtain information about
pagination process., (*12)
{{ simple_paginator_render('my_controller_route') }}
You can to customize paginator view as follows:, (*13)
{{ simple_paginator_render('my_controller_route', null, { params }) }}
where params
may be:, (*14)
routeParams
, the params needed by the controller route (default: {}
), (*15)
container_class
, the CSS class applied to the <ul>
element that wraps the paginator (default: simple_paginator
), (*16)
firstPageText
, the text shown on the first page link (default: first
), (*17)
firstEnabledClass
, the CSS class applied to the first page link if there is a first page (default: first
)firstDisabledClass
, the CSS class applied to the first page text when there is no first page (default: first_disabled
), (*18)
previousPageText
, the text shown on the previous page link (default: previous
), (*19)
previousEnabledClass
, the CSS class applied to the previous page link if there is a previous page (default: left
)previousDisabledClass
, the CSS class applied to the previous page text when there is no previous page (default: left_disabled
), (*20)
currentClass
, the CSS class applied to the <li>
element that wraps the current page link (default: current
), (*21)
nextPageText
, the text shown on the next page link (default: next
), (*22)
nextEnabledClass
, the CSS class applied to the next page link if there is a next page (default: right
)nextDisabledClass
, the CSS class applied to the next page text when there is no next page (default: right_disabled
), (*23)
lastPageText
, the text shown on the last page link (default: last
), (*24)
lastEnabledClass
, the CSS class applied to the last page link if there is a last page (default: last
)lastDisabledClass
, the CSS class applied to the last page text when there is no last page (default: last_disabled
)For example, if you want to customize paginator view to show a route that receive a
parameter id
and you want to change container class:, (*25)
{{ simple_paginator_render('my_controller_route', null, { 'routeParams' : {'id' : id}, 'container_class' : 'custom_simple_paginator_class' }) }}
If your needs are out of this sight you can customize it in your own view:, (*26)
{{ simple_paginator_render('my_controller_route', null, {....}, 'MyBundle:MyViewFolder:MyViewFile.html.twig') }}
To create MyBundle:MyViewFolder:MyViewFile.html.twig
copy from default template that is included inside the bundle
Resources\views\Paginator\simple-paginator-list-view.html.twig
and customize it in your own Bundle., (*27)
For example, if you want only to show paginator numbers, your template sounds like this, (*28)
MyBundle\Resources\views\MyViewFolder\MyViewFile.html.twig
:, (*29)
<ul class="{{ container_class }}"> <!-- NUMBERS --> {% for page in minPage..maxPage %} {% if page == currentPage %} <li class="{{ currentClass }}"> {{ page }} </li> {% else %} {% set rParams = {'page': page, 'paginatorId': id} | merge(routeParams) %} <li> <a href="{{ path(route, rParams) }}">{{ page }}</a> </li> {% endif %} {% endfor %} </ul>
SimplePaginatorBundle
supports multiple paginators, you should specify an id in your controller and view calls. Note
that you can modify the particular properties of each paginator., (*30)
<?php class MyController extends Controller { public function listAction() { $paginator = $this->get('ideup.simple_paginator'); $paginator->setItemsPerPage(25, 'users'); $users = $paginator->paginate($em->getRepository('MyBundle:User')->findByMyCriteriaDQL(), 'users')->getResult(); $paginator->setItemsPerPage(5, 'groups'); $groups = $paginator->paginate($em->getRepository('MyBundle:User')->findByMyCriteriaDQL(), 'groups')->getResult(); $vars = array( 'users' => $users, 'groups' => $groups, 'paginator' => $paginator); return $this->render('MyBundle:User:list.html.twig', $vars); } }
In the view you also need to specify the paginator id:, (*31)
<?php class MyController extends Controller { public function listAction() { $paginator = $this->get('ideup.simple_paginator'); $users = $paginator ->setItemsPerPage(25, 'users'); ->paginate($em->getRepository('MyBundle:User')->findByMyCriteriaDQL(), 'users') ->getResult() ; // Now also we can paginate arrays $allGroups = array('group1', 'group2', 'group3', 'group4', 'group5'); $groups = $paginator ->setItemsPerPage(3, 'groups') ->paginate($allGroups, 'groups') ->getResult() ; $vars = array( 'users' => $users, 'groups' => $groups, ); return $this->render('MyBundle:User:list.html.twig', $vars); } }
In the view you also need to specify the paginator id:, (*32)
{{ simple_paginator_render('my_controller_route', 'users', {....}) }} {{ simple_paginator_render('my_controller_route', 'groups', {....}) }}
v0.91, (*33)
Added support to paginate arrays., (*34)
Changed setter methods. Now this methods return paginate object., (*35)
Added twig support., (*36)
v0.9, (*37)
Added dependency to DoctrineExtensions\Paginate to handle proper pagination (see https://github.com/beberlei/DoctrineExtensions), (*38)
Added support to paginate multiple lists at once, (*39)
Changed the Paginador class name to Paginator, this is how services.xml defines our service, with a parameter set to the class implementing pagination and passing a service id, (*40)
<parameters> <parameter key="simple_paginador.class">Ideup\SimplePaginatorBundle\Paginator\Paginator</parameter> </parameters> <services> <service id="ideup.simple_paginator" class="%simple_paginator.class%"> <argument type="service" id="request" strict="false" /> </service> </services>
Symfony 2 paginator bundle.
paginator recordset