Getting a Collection of Resources
Last updated
Last updated
This section is deprecated. However, as of now, the Sylius E-Commerce project is still resorting to this configuration so you might want to check it out.
This section is deprecated. However, as of now, the Sylius E-Commerce project is still resorting to this configuration so you might want to check it out.
To get a paginated list of Books, we will use indexAction of our controller. In the default scenario, it will return an instance of paginator, with a list of Books.
When you go to /books
, the ResourceController will use the repository (app.repository.book
) to create a paginator. The default template will be rendered - App:Book:index.html.twig
with the paginator as the books
variable.
A paginator can be a simple array, if you disable the pagination, otherwise it is an instance of Pagerfanta\Pagerfanta
which is a used to manage the pagination.
Just like for the showAction, you can override the default template and criteria.
This action will render a custom template with a paginator only for disabled Books.
Except filtering, you can also sort Books.
Under that route, you can paginate over the Books by their score.
If you want to paginate your resources you need to use EntityRepository::getPaginator($queryBuilder)
. It will transform your doctrine query builder into Pagerfanta\Pagerfanta
object.
You can also control the "max per page" for paginator, using paginate
parameter.
This will paginate 5 books per page, where 10 is the default.
Pagination is handy, but you do not always want to do it, you can disable pagination and simply request a collection of resources.
That action will return the top 3 books by score, as the books
variable.
Remember that you can use controller's Fully Qualified Class Name (App\Controller\BookController
) instead of id app.controller.book
You can define your own repository method too, you can use the same way explained in .
Read more about .