Run composer update to install the package. Then you'll need to register the bundle in your app/AppKernel.php:, (*5)
$bundles = array(
// ...
new AshleyDawson\SimplePaginationBundle\AshleyDawsonSimplePaginationBundle(),
);
Basic Usage
The simplest collection we can use the paginator service on is an array. Please see below for an extremely
simple example of the paginator operating on an array. This shows the service paginating over an array of
12 items., (*6)
namespace Acme\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class WelcomeController extends Controller
{
public function indexAction()
{
// Get the paginator service from the container
$paginator = $this->get('ashley_dawson_simple_pagination.paginator');
// Build a mock set of items to paginate over
$items = array(
'Banana',
'Apple',
'Cherry',
'Lemon',
'Pear',
'Watermelon',
'Orange',
'Grapefruit',
'Blackcurrant',
'Dingleberry',
'Snosberry',
'Tomato',
);
// Set the item total callback, simply returning the total number of items
$paginator->setItemTotalCallback(function () use ($items) {
return count($items);
});
// Add the slice callback, simply slicing the items array using $offset and $length
$paginator->setSliceCallback(function ($offset, $length) use ($items) {
return array_slice($items, $offset, $length);
});
// Perform the pagination, passing the current page number from the request
$pagination = $paginator->paginate((int)$this->get('request')->query->get('page', 1));
// Pass the pagination object to the view for rendering
return $this->render('AcmeDemoBundle:Welcome:index.html.twig', array(
'pagination' => $pagination,
));
}
}
And in the twig view, it looks like this:, (*7)
...
{# Iterate over items for the current page, rendering each one #}
{% for item in pagination.items %}
{{ item }}
{% endfor %}
{# Iterate over the page list, rendering the page links #}
{% for page in pagination.pages %}
{{ page }} |
{% endfor %}
...
You can override the "items per page" and "pages in range" options at runtime by passing values to the paginator like this:, (*8)
Please note that this is a very simple example, some advanced use-cases and interfaces are coming up (see below)., (*9)
Doctrine Example
I've expanded the example above to use Doctrine instead of a static array of items:, (*10)
namespace Acme\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class WelcomeController extends Controller
{
public function indexAction()
{
// Get the paginator service from the container
$paginator = $this->get('ashley_dawson_simple_pagination.paginator');
// Create a Doctrine query builder
$manager = $this->getDoctrine()->getManager();
$query = $manager->createQueryBuilder();
// Build the initial query, including any special filters
$query
->from('AcmeDemoBundle:Film', 'f')
->where('f.releaseAt > :threshold')
->setParameter('threshold', new \DateTime('1980-08-16'))
;
// Pass the item total callback
$paginator->setItemTotalCallback(function () use ($query) {
// Run the count of all records
$query
->select('COUNT(f.id)')
;
// Return the total item count
return (int)$query->getQuery()->getSingleScalarResult();
});
// Pass the slice callback
$paginator->setSliceCallback(function ($offset, $length) use ($query) {
// Select and slice the data
$query
->select('f')
->setFirstResult($offset)
->setMaxResults($length)
;
// Return the collection
return $query->getQuery()->getResult();
});
// Finally, paginate using the current page number
$pagination = $paginator->paginate((int)$this->get('request')->query->get('page', 1));
// Pass the pagination object to the view
return $this->render('AcmeDemoBundle:Welcome:index.html.twig', array(
'pagination' => $pagination,
));
}
}
And in the twig view, it looks like this:, (*11)
...
{# Iterate over films (Doctrine results) for the current page, rendering each one #}
{% for film in pagination.items %}
{{ film.title }}
{% endfor %}
{# Use the pagination view helper to render the page navigation #}
I've provided a handy twig function to render the built in pagination template. The default template
can be configured in your app/config/config.yml or simply overridden as an argument in the twig function., (*13)
The arguments passed to the twig function are as follows:, (*14)