Skip to content

Instantly share code, notes, and snippets.

@Nugjii
Forked from atillay/DemoController.php
Last active January 30, 2020 09:03
Show Gist options
  • Save Nugjii/c284ce781ea32cf878c394db8eee2c49 to your computer and use it in GitHub Desktop.
Save Nugjii/c284ce781ea32cf878c394db8eee2c49 to your computer and use it in GitHub Desktop.
Symfony 4 Pagination Service
{% set _currentPage = app.request.query.get('p') ?: 1 %}
{% set _currentRoute = app.request.attributes.get('_route') %}
{% set _currentParams = app.request.query.all %}
{% if lastPage > 1 %}
<nav>
<ul class="pagination">
<li class="page-item{% if _currentPage == 1 %} disabled{% endif %}"><a class="page-link" href="{{ path(_currentRoute, _currentParams|merge({p: 1})) }}" data-toggle="tooltip" data-placement="bottom" title="{% trans %}First{% endtrans %}">&laquo;</a></li>
<li class="page-item{% if _currentPage == 1 %} disabled{% endif %}"><a class="page-link" href="{{ path(_currentRoute, _currentParams|merge({p: _currentPage - 1})) }}" data-toggle="tooltip" data-placement="bottom" title="{% trans %}Previous{% endtrans %}">&lsaquo;</a></li>
{% set f = 1 %}
{% if _currentPage > 5 %}
{% set f = _currentPage - 5 %}
{% endif %}
{% set l = lastPage %}
{% if lastPage - _currentPage > 5 %}
{% set l = _currentPage + 5 %}
{% endif %}
{% for i in f..l %}
<li class="page-item {% if i == _currentPage %}active{% endif %}">
<a class="page-link" href="{{ path(_currentRoute, _currentParams|merge({p: i})) }}">{{ i }}</a>
</li>
{% endfor %}
<li class="page-item{% if _currentPage == lastPage %} disabled{% endif %}"><a class="page-link" href="{{ path(_currentRoute, _currentParams|merge({p: _currentPage + 1})) }}" data-toggle="tooltip" data-placement="bottom" title="{% trans %}Next{% endtrans %}">&rsaquo;</a></li>
<li class="page-item{% if _currentPage == lastPage %} disabled{% endif %}"><a class="page-link" href="{{ path(_currentRoute, _currentParams|merge({p: lastPage})) }}" data-toggle="tooltip" data-placement="bottom" title="{% trans %}Last{% endtrans %}">&raquo;</a></li>
</ul>
</nav>
{% endif %}
<?php
namespace App\Controller;
use App\Entity\Demo;
use App\Service\PaginationService;
use Symfony\Component\HttpFoundation\Request;
class DemoController extends AdminController
{
const ITEMS_PER_PAGE = 50;
public function index(Request $request, PaginationService $pagination)
{
$entityManager = $this->getDoctrine()->getManager();
$query = $entityManager->getRepository(Demo::class)->createQueryBuilder('a')->getQuery();
$results = $pagination->paginate($query, $request, self::ITEMS_PER_PAGE);
return $this->render('admin/school/index.html.twig', [
'schools' => $results,
'lastPage' => $pagination->lastPage($results)
]);
}
}
<?php
namespace App\Service;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Symfony\Component\HttpFoundation\Request;
class PaginationService
{
/**
* @param QueryBuilder|Query $query
* @param Request $request
* @param int $limit
* @return Paginator
*/
public function paginate($query, Request $request, int $limit): Paginator
{
$currentPage = $request->query->getInt('p') ?: 1;
$paginator = new Paginator($query);
$paginator
->getQuery()
->setFirstResult($limit * ($currentPage - 1))
->setMaxResults($limit);
return $paginator;
}
/**
* @param Paginator $paginator
* @return int
*/
public function lastPage(Paginator $paginator): int
{
return ceil($paginator->count() / $paginator->getQuery()->getMaxResults());
}
/**
* @param Paginator $paginator
* @return int
*/
public function total(Paginator $paginator): int
{
return $paginator->count();
}
/**
* @param Paginator $paginator
* @return bool
*/
public function currentPageHasNoResult(Paginator $paginator): bool
{
return !$paginator->getIterator()->count();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment