-
Notifications
You must be signed in to change notification settings - Fork 6
[WIP] Pagination example with knplabs/knp-pagination-bundle #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c801657
30e2cef
960c038
366bf68
c5cb10c
eaebc17
d649640
75ab047
ead0c9e
d835726
c99f2fb
82a7ff9
133dc45
d1a915f
8390e69
c928c13
571bca4
f045490
b851da2
4dd3de0
d2f30e4
753f362
955cb9d
c07b911
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
|
|
||
| use Runroom\RenderEventBundle\Renderer\PageRenderer; | ||
| use Runroom\SamplesBundle\BasicEntities\Service\BookService; | ||
| use Symfony\Component\HttpFoundation\Request; | ||
| use Symfony\Component\HttpFoundation\Response; | ||
|
|
||
| class BookController | ||
|
|
@@ -52,4 +53,15 @@ public function book(string $slug): Response | |
| $model | ||
| ); | ||
| } | ||
|
|
||
| public function list(Request $request): Response | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. porqué no usas el mismo método que ya teniamos de |
||
| { | ||
| $page = (int) $request->get('page', 1); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. creo que si haces: |
||
| $model = $this->service->getBooksListViewModel($page); | ||
|
|
||
| return $this->renderer->renderResponse( | ||
| '@RunroomSamples/BasicEntities/book-list.html.twig', | ||
| $model | ||
| ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,18 +13,30 @@ | |
|
|
||
| namespace Runroom\SamplesBundle\BasicEntities\Service; | ||
|
|
||
| use Knp\Bundle\PaginatorBundle\Pagination\SlidingPaginationInterface; | ||
| use Knp\Component\Pager\PaginatorInterface; | ||
| use Runroom\SamplesBundle\BasicEntities\Repository\BookRepository; | ||
| use Runroom\SamplesBundle\BasicEntities\ViewModel\BooksListViewModel; | ||
| use Runroom\SamplesBundle\BasicEntities\ViewModel\BooksViewModel; | ||
| use Runroom\SamplesBundle\BasicEntities\ViewModel\BookViewModel; | ||
|
|
||
| class BookService | ||
| { | ||
| /** @var int */ | ||
| private const MAX_RESULT = 5; | ||
|
|
||
| /** @var BookRepository */ | ||
| private $repository; | ||
|
|
||
| public function __construct(BookRepository $repository) | ||
| { | ||
| /** @var PaginatorInterface */ | ||
| private $paginator; | ||
|
|
||
| public function __construct( | ||
| BookRepository $repository, | ||
| PaginatorInterface $paginator | ||
| ) { | ||
| $this->repository = $repository; | ||
| $this->paginator = $paginator; | ||
| } | ||
|
|
||
| public function getBooksViewModel(): BooksViewModel | ||
|
|
@@ -46,4 +58,16 @@ public function getBookViewModel(string $slug): BookViewModel | |
|
|
||
| return $model; | ||
| } | ||
|
|
||
| public function getBooksListViewModel(int $page): BooksListViewModel | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aquí lo mismo, podríamos reusar el método ya existente: |
||
| { | ||
| $queryBuilder = $this->repository->getBooksQueryBuilder(); | ||
|
|
||
| $pagination = $this->paginator->paginate($queryBuilder, $page, self::MAX_RESULT); | ||
| \assert($pagination instanceof SlidingPaginationInterface); | ||
| $model = new BooksListViewModel(); | ||
| $model->setPagination($pagination); | ||
|
|
||
| return $model; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /* | ||
| * This file is part of the RunroomSamplesBundle. | ||
| * | ||
| * (c) Runroom <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Runroom\SamplesBundle\BasicEntities\ViewModel; | ||
|
|
||
| use Knp\Bundle\PaginatorBundle\Pagination\SlidingPaginationInterface; | ||
| use Runroom\SamplesBundle\BasicEntities\Entity\Book; | ||
|
|
||
| class BooksListViewModel | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Podríamos reusar el viewModel ya existente. |
||
| { | ||
| /** | ||
| * @phpstan-var SlidingPaginationInterface<Book> | ||
| * @psalm-var SlidingPaginationInterface | ||
| */ | ||
| protected $pagination; | ||
|
|
||
| /** @var array<mixed> */ | ||
| protected $paginationData; | ||
|
|
||
| /** @phpstan-return SlidingPaginationInterface<Book>|null | ||
| * @psalm-return SlidingPaginationInterface|null | ||
| */ | ||
| public function getPagination(): ?SlidingPaginationInterface | ||
| { | ||
| return $this->pagination; | ||
| } | ||
|
|
||
| /** | ||
| * @phpstan-param SlidingPaginationInterface<Book> $pagination | ||
| * @psalm-param SlidingPaginationInterface $pagination | ||
| * */ | ||
| public function setPagination(SlidingPaginationInterface $pagination): self | ||
| { | ||
| $this->pagination = $pagination; | ||
| $this->paginationData = $pagination->getPaginationData(); | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| public function getPreviousPage(): ?int | ||
| { | ||
| return $this->getPaginationData('previous'); | ||
| } | ||
|
|
||
| public function getNextPage(): ?int | ||
| { | ||
| return $this->getPaginationData('next'); | ||
| } | ||
|
|
||
| public function getPaginationData(string $data): ?int | ||
| { | ||
| return $this->paginationData[$data] ?? null; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,10 @@ runroom_samples.basic_entities.book: | |
| path: /book/{slug} | ||
| controller: Runroom\SamplesBundle\BasicEntities\Controller\BookController::book | ||
|
|
||
| runroom_samples.basic_entities.books_list: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Podríamos reusar la url ya existente. |
||
| path: /pagination | ||
| controller: Runroom\SamplesBundle\BasicEntities\Controller\BookController::list | ||
|
|
||
| # Forms | ||
| runroom_samples.forms.contact: | ||
| path: /contact | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| {% extends '@RunroomSamples/base.html.twig' %} | ||
|
|
||
| {% block content %} | ||
| <div class="u-wrapper"> | ||
| <div class="list"> | ||
| <ul> | ||
| {% for book in model.pagination.items %} | ||
| <div class="book-item"> | ||
| <li> | ||
| <a href="{{ path('runroom_samples.basic_entities.book', {slug: book.slug}) }}">{{book.title}}</a> | ||
| </li> | ||
| </div> | ||
| {% endfor %} | ||
| </ul> | ||
| </div> | ||
| <div class="pagination"> | ||
| {{ knp_pagination_render(model.pagination) }} | ||
| </div> | ||
| </div> | ||
| {% endblock content %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yo este cambio lo desharía, no es consistente con el resto de admins.