Sylius Stack
  • Sylius Stack Documentation
  • Getting started
  • Cookbook
    • How to customize your admin panel
      • Basic operations
      • Customizing your grids
      • Customizing the logo
      • Customizing the menu
      • Configuring the security access
      • Customizing the page titles
    • How to use in a DDD architecture
      • Architecture overview
      • Resource configuration
      • Basic operations
      • Operation using a grid
  • Admin UI
    • Getting started
  • Bootstrap Admin UI
    • Getting started
  • Resource
    • Resource Bundle documentation
      • Installation
      • Create new resource
      • Configure your resource
      • Configure your operations
      • Validation
      • Redirect
      • Resource factories
      • Providers
      • Processors
      • Responders
      • Legacy Resource Documentation
        • Configuration
        • Services
        • Routing
        • Forms
        • Getting a Single Resource
        • Getting a Collection of Resources
        • Creating Resources
        • Updating Resources
        • Deleting Resources
        • Configuring a state machine
        • Configuration Reference
  • Grid
    • Grid Bundle documentation
      • Installation
      • Creating your first grid
      • Configuring Fields
      • Field types
      • Creating a custom Field Type
      • Creating a custom Action
      • Creating a custom Bulk Action
      • Filters
      • Creating a custom Filter
      • Advanced configuration
      • Configuration Reference
  • 🍀Twig Extra
    • Getting started
  • 🌱Twig Hooks
    • Getting started
    • Passing data to your hookables
    • Making your hookables configurable
    • Autoprefixing feature
    • Composable Layouts with a predictable structure
    • Advanced
      • Ergonomic work with hooks
      • Metadata objects
      • Multiple hooks inside a single template
      • Overriding hookables
Powered by GitBook
On this page
  • Overriding the Template and Criteria
  • Sorting
  • Using a Custom Repository Method
  • Note
  • Changing the "Max Per Page" Option of Paginator
  • Disabling Pagination - Getting a Simple Collection
  • Changing the serialization groups of the elements in a paginated response
  • Configuration Reference
  1. Resource
  2. Resource Bundle documentation
  3. Legacy Resource Documentation

Getting a Collection of Resources

PreviousGetting a Single ResourceNextCreating Resources

Last updated 4 months ago

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.

config/routes.yaml
app_book_index:
    path: /books
    methods: [GET]
    defaults:
        _controller: app.controller.book::indexAction

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.

Overriding the Template and Criteria

Just like for the showAction, you can override the default template and criteria.

config/routes.yaml
app_book_index_inactive:
    path: /books/disabled
    methods: [GET]
    defaults:
        _controller: app.controller.book::indexAction
        _sylius:
            filterable: true
            criteria:
                enabled: false
            template: Book/disabled.html.twig

This action will render a custom template with a paginator only for disabled Books.

Sorting

Except filtering, you can also sort Books.

config/routes.yaml
app_book_index_top:
    path: /books/top
    methods: [GET]
    defaults:
        _controller: app.controller.book::indexAction
        _sylius:
            sortable: true
            sorting:
                score: desc
            template: Book/top.html.twig

Under that route, you can paginate over the Books by their score.

Using a Custom Repository Method

Note

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.

Changing the "Max Per Page" Option of Paginator

You can also control the "max per page" for paginator, using paginate parameter.

config/routes.yaml
app_book_index_top:
    path: /books/top
    methods: [GET]
    defaults:
        _controller: app.controller.book::indexAction
        _sylius:
            paginate: 5
            sortable: true
            sorting:
                score: desc
            template: Book/top.html.twig

This will paginate 5 books per page, where 10 is the default.

Disabling Pagination - Getting a Simple Collection

Pagination is handy, but you do not always want to do it, you can disable pagination and simply request a collection of resources.

config/routes.yaml
app_book_index_top3:
    path: /books/top
    methods: [GET]
    defaults:
        _controller: app.controller.book::indexAction
        _sylius:
            paginate: false
            limit: 3
            sortable: true
            sorting:
                score: desc
            template: Book/top3.html.twig

That action will return the top 3 books by score, as the books variable.

Changing the serialization groups of the elements in a paginated response

config/routes.yaml
app_book_index:
    path: /{author}/books
    methods: [GET]
    defaults:
        _controller: app.controller.book::indexAction
        _sylius:
            serialization_groups: { 0: Default, items: [ Custom ] }

Configuration Reference

config/routes.yaml
app_book_index:
    path: /{author}/books
    methods: [GET]
    defaults:
        _controller: app.controller.book::indexAction
        _sylius:
            template: Author/books.html.twig
            repository:
                method: createPaginatorByAuthor
                arguments: [$author]
            criteria:
                enabled: true
                author.name: $author
            paginate: false # Or: 50
            limit: 100 # Or: false
            serialization_groups: [Custom, Details]
            serialization_version: 1.0.2

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 .

Library
show_resource
nested serialization groups
Go back to the documentation's index