Based on KnpLabs' PaginatorBundle.
Add to composer.json
{
"require": {
"dezull/dbal-paginator-service-provider": "dev-master"
}
}- When registering
TwigServiceProvider, add the paginator's template path (create your own or just use KnpPaginatorBundle's).
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => array(
/* Your other templates */
__DIR__.'/../vendor/knplabs/knp-paginator-bundle/Knp/Bundle/PaginatorBundle/Resources/views',
),
));- Register
DBALPaginatorServiceProvider.
$app->register(new Dezull\Silex\Provider\DBALPaginatorServiceProvider\DBALPaginatorServiceProvider(), array(
/* The following assumes you use the template path as in step #1 */
'dezull.dbal_paginator.template.pagination' => 'Pagination/twitter_bootstrap_pagination.html.twig',
'dezull.dbal_paginator.template.sortable' => 'Pagination/sortable_link.html.twig',
));- In your controller
public function indexAction(Request $request, Application $app)
{
$page = (int) $request->query->get('page', 1);
$sortKey = $request->query->get('sort', 's.id');
$direction = $request->query->get('direction', 'desc');
/* Doctrine DBAL QueryBuilder */
$qb = $app['db']->createQueryBuilder()
->select('s.*')
->from('sometable', 's')
->orderBy($sortKey, $direction);
$pagination = $app['dezull.dbal_paginator']->paginate(
$qb,
$page,
20 /* per page limit */
);
return $app['twig']->render('Foo/index.html.twig', array(
'pagination' => $pagination,
));
}- To render the pagination in the template
{{ dezull_dbal_pagination_render(pagination) }}- Remove dependency on KnpPaginatorBundle