, (*1)
This repository is for AnimeDbPaginationBundle. GpsLabPaginationBundle, the new version of PaginationBundle, has been released and is available at https://github.com/gpslab/pagination-bundle., (*2)
AnimeDbPaginationBundle is no longer maintained and is now end of life., (*3)
Installation
Pretty simple with Composer, run:, (*4)
composer require anime-db/pagination-bundle
Add PaginatorBundle to your application kernel, (*5)
// app/AppKernel.php
public function registerBundles()
{
return array(
// ...
new AnimeDb\Bundle\PaginationBundle\AnimeDbPaginationBundle(),
// ...
);
}
Configuration example
You can configure default templates, (*6)
anime_db_pagination:
max_navigate: 5 # default page range used in pagination control
template: 'AnimeDbPaginationBundle::pagination.html.twig' # sliding pagination controls template
Usage
namespace Acme\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration;
class ArticleController extends Controller
{
/**
* @Configuration\Route("/article/", name="article_index")
* @Configuration\Method({"GET"})
*
* @param Request $request
*
* @return Response
*/
public function indexAction(Request $request)
{
$per_page = 100; // articles per page
$em = $this->get('doctrine.orm.entity_manager');
$router = $this->get('router');
// get total articles
$total = (int)$em
->createQueryBuilder()
->select('COUNT(*)')
->from('AcmeDemoBundle:Article', 'a')
->getQuery()
->getSingleScalarResult();
// build pagination
$pagination = $this
->get('pagination')
->paginate(
ceil($total / $per_page), // total pages
$request->query->get('page') // correct page
)
->setPageLink(function($page) use ($router) { // build page link
return $router->generate('article_index', ['page' => $page]);
})
->setFirstPageLink($router->generate('article_index')); // build link for first page
// get articles chunk
$articles = $em
->createQueryBuilder()
->select('*')
->from('AcmeDemoBundle:Article', 'a')
->setFirstResult(($pagination->getCurrentPage() - 1) * $per_page)
->setMaxResults($per_page)
->getQuery()
->getResult();
// template parameters
return $this->render('AcmeDemoBundle:Article:index.html.twig', [
'total' => $total,
'articles' => $articles,
'pagination' => $pagination
]);
}
}
From QueryBuilder
namespace Acme\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration;
use Acme\DemoBundle\Entity\Article;
class ArticleController extends Controller
{
/**
* @var int
*/
const PER_PAGE = 100;
/**
* @Configuration\Route("/article/", name="article_index")
* @Configuration\Method({"GET"})
*
* @param Request $request
*
* @return Response
*/
public function indexAction(Request $request)
{
// create get articles query
// would be better move this query to repository class
$query = $this
->getDoctrine()
->getRepository('AcmeDemoBundle:Article')
->createQueryBuilder('a')
->where('a.status = :status')
->setParameter('status', Article::STATUS_ENABLED);
// build pagination
$pagination = $this
->get('pagination')
->paginateQuery(
$query, // query
self::PER_PAGE, // articles per page
$request->query->get('page') // correct page
)
->setPageLink(function($page) { // build page link
return $this->generateUrl('article_index', ['page' => $page]);
})
->setFirstPageLink($this->generateUrl('article_index')); // build link for first page
// template parameters
return $this->render('AcmeDemoBundle:Article:index.html.twig', [
'total' => $pagination->getTotalPages(), // total pages
'articles' => $query->getQuery()->getResult(), // get articles chunk
'pagination' => $pagination
]);
}
}
View
{# total items #}
{{ total }}
{# list articles #}
{% for article in articles %}
{{ article.id }} |
{{ article.title }} |
{{ article.date|date('Y-m-d, H:i:s') }} |
{% endfor %}
{# display navigation #}
{{ pagination_render(pagination) }}
Custom view
{# display navigation #}
{{ pagination_render(pagination, 'custom_pagination.html.twig', {custom_var: 'foo'}) }}
Example Material Design template for pagination, (*7)
{# custom_pagination.html.twig #}
{# print 'foo' #}
{{ custom_var }}
{% if pagination.total > 1 %}
{% spaceless %}
{% endspaceless %}
{% endif %}
License
This bundle is under the MIT license. See the complete license in the file: LICENSE, (*8)