Last active
July 24, 2024 17:19
-
-
Save atillay/f309aff35a6e4dd19a16c92caa8669ab to your computer and use it in GitHub Desktop.
Symfony 4 Pagination Service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Service; | |
| use Doctrine\ORM\Query; | |
| use Doctrine\ORM\QueryBuilder; | |
| use Doctrine\ORM\Tools\Pagination\Paginator; | |
| use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | |
| class Pagination | |
| { | |
| /** USAGE ** | |
| $limit = 10; | |
| $currentPage = $request->query->getInt('p') ?: 1; | |
| $myQuery = $em->getRepository(MyRepo::class)->myQuery(); | |
| $results = $pagination->paginate($myQuery, $limit, $currentPage); | |
| $lastPage = $pagination->lastPage($results); | |
| $total = $results->count(); | |
| ****/ | |
| /** | |
| * @param QueryBuilder|Query $query | |
| * @param int $limit | |
| * @param int $currentPage | |
| * @return Paginator | |
| */ | |
| public function paginate($query, int $limit, int $currentPage): Paginator | |
| { | |
| $paginator = new Paginator($query); | |
| $paginator | |
| ->getQuery() | |
| ->setFirstResult($limit * ($currentPage - 1)) | |
| ->setMaxResults($limit); | |
| if ($this->hasNoResult($paginator)) { | |
| throw new NotFoundHttpException(); | |
| } | |
| return $paginator; | |
| } | |
| /** | |
| * @param Paginator $paginator | |
| * @return int | |
| */ | |
| public function lastPage(Paginator $paginator): int | |
| { | |
| return ceil($paginator->count() / $paginator->getQuery()->getMaxResults()); | |
| } | |
| /** | |
| * @param Paginator $paginator | |
| * @return bool | |
| */ | |
| public function hasNoResult(Paginator $paginator): bool | |
| { | |
| return !$paginator->getIterator()->count(); | |
| } | |
| } |
Some improve for extrem pagination
{% set _currentPage = app.request.query.get('p') ?: 1 %}
{% set _currentRoute = app.request.attributes.get('_route') %}
{% set _currentParams = app.request.query.all %}
{% set nearbyPagesLimit = 4 %}
{% if lastPage > 1 %}
<div class="col-md-12">
<nav>
<ul class="pagination">
{% if _currentPage > 1 %}
<li class="page-item">
<a class="page-link"
href="{{ path(_currentRoute, _currentParams|merge({p: _currentPage - 1})) }}"
aria-label="Previous">
<span aria-hidden="true">«</span>
<span class="sr-only">Précedent</span>
</a>
</li>
{% endif %}
{% for i in 1..lastPage %}
{% if 0 == (_currentPage - nearbyPagesLimit) - loop.index %} {# dot before #}
<li class="disabled"><a class="page-link" href="#">...</a></li>
{% elseif 0 == (_currentPage + nearbyPagesLimit) - loop.index %} {# dot after #}
<li class="disabled"><a class="page-link" href="#">...</a></li>
{% elseif 0 < (_currentPage - nearbyPagesLimit) - loop.index %} {# hide all before #}
{% elseif 0 > (_currentPage + nearbyPagesLimit) - loop.index %} {# hide all after #}
{% else %}
<li {% if _currentPage == loop.index %} class="page-item active"{% endif %}>
<a class="page-link" href="{{ path(_currentRoute, _currentParams|merge({p: i})) }}">{{ loop.index }}</a>
</li>
{% endif %}
{% endfor %}
{% if _currentPage < lastPage %}
<li class="page-item">
<a class="page-link"
href="{{ path(_currentRoute, _currentParams|merge({p: _currentPage + 1})) }}"
aria-label="Next">
<span aria-hidden="true">»</span>
<span class="sr-only">Suivant</span>
</a>
</li>
{% endif %}
</ul>
</nav>
</div>
{% endif %}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
SF4 use like this...
$query = $this->getDoctrine()->getManager()->getRepository(Demo::class)->createQueryBuilder('d');