-
-
Save i-amolo/0392e808536158a4b3e2c866edf8c90e to your computer and use it in GitHub Desktop.
Simple Doctrine Paginator and example usage for Symfony. Простейшая постраничная навигация в Symfony.
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\Controller; | |
| use App\Utils\Paginator; | |
| use Doctrine\ORM\EntityManagerInterface; | |
| use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | |
| use Symfony\Component\HttpFoundation\Request; | |
| use Symfony\Component\HttpFoundation\Response; | |
| class PostController extends AbstractController | |
| { | |
| public function index(Request $request, Paginator $paginator, EntityManagerInterface $em): Response | |
| { | |
| // Query or Query Builder | |
| // NOT RESULTS! | |
| $query = $em->getRepository(Post::class)->findActivePostsQuery(); | |
| $paginator->paginate($query, $request->query->getInt('page', 1)); | |
| return $this->render('index.html.twig', [ | |
| 'paginator' => $paginator, | |
| ]); | |
| } | |
| } |
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
| {% for post in paginator.items %} | |
| {{ post.title }} | |
| {% endfor %} | |
| {% include 'paginator.html.twig' %} |
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\Utils; | |
| use Doctrine\ORM\Query; | |
| use Doctrine\ORM\QueryBuilder; | |
| use Doctrine\ORM\Tools\Pagination\Paginator as OrmPaginator; | |
| class Paginator | |
| { | |
| /** | |
| * @var integer | |
| */ | |
| private $total; | |
| /** | |
| * @var integer | |
| */ | |
| private $lastPage; | |
| private $items; | |
| /** | |
| * @param QueryBuilder|Query $query | |
| * @param int $page | |
| * @param int $limit | |
| * @return Paginator | |
| */ | |
| public function paginate($query, int $page = 1, int $limit = 10): Paginator | |
| { | |
| $paginator = new OrmPaginator($query); | |
| $paginator | |
| ->getQuery() | |
| ->setFirstResult($limit * ($page - 1)) | |
| ->setMaxResults($limit); | |
| $this->total = $paginator->count(); | |
| $this->lastPage = (int) ceil($paginator->count() / $paginator->getQuery()->getMaxResults()); | |
| $this->items = $paginator; | |
| return $this; | |
| } | |
| public function getTotal(): int | |
| { | |
| return $this->total; | |
| } | |
| public function getLastPage(): int | |
| { | |
| return $this->lastPage; | |
| } | |
| public function getItems() | |
| { | |
| return $this->items; | |
| } | |
| } |
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
| {% set _currentPage = app.request.query.get('page') ?: 1 %} | |
| {% set _currentRoute = app.request.attributes.get('_route') %} | |
| {% set _lastPage = paginator.lastPage %} | |
| {% set _currentParams = app.request.query.all|merge(app.request.attributes.get('_route_params')) %} | |
| {% if paginator.lastPage > 1 %} | |
| <nav> | |
| <ul class="pagination justify-content-center"> | |
| <li class="page-item{{ _currentPage <= 1 ? ' disabled' : '' }}"> | |
| <a class="page-link" href="{{ path(_currentRoute, _currentParams|merge({page: _currentPage - 1})) }}" aria-label="Previous"> | |
| « Назад | |
| </a> | |
| </li> | |
| {% for i in 1..paginator.lastPage %} | |
| <li class="page-item {% if i == _currentPage %}active{% endif %}"> | |
| <a class="page-link" href="{{ path(_currentRoute, _currentParams|merge({page: i})) }}">{{ i }}</a> | |
| </li> | |
| {% endfor %} | |
| <li class="page-item {{ _currentPage >= paginator.lastPage ? ' disabled' : '' }}"> | |
| <a class="page-link" href="{{ path(_currentRoute, _currentParams|merge({page: _currentPage + 1})) }}" aria-label="Next"> | |
| Далее » | |
| </a> | |
| </li> | |
| </ul> | |
| </nav> | |
| {% endif %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment