1. Grid view trong Sylius dùng để làm gì?

  • Hiển thị danh sách resource (sản phẩm, khách hàng, order…) theo dạng bảng.

  • Tự động hỗ trợ:

    • Pagination (phân trang)

    • Sorting (sắp xếp)

    • Filtering (lọc theo trường)

    • Actions (bulk actions hoặc row actions, ví dụ: Edit/Delete)

  • Cho phép tùy biến cột hiển thị (columns).

  • Hỗ trợ export (CSV, Excel) thông qua một số plugin.

2. Cấu hình Grid trong Sylius

Mỗi grid được định nghĩa trong file YAML, ví dụ:

# config/packages/_sylius.yaml
sylius_grid:
    grids:
        app_admin_customer:
            driver:
                name: doctrine/orm
                options:
                    class: App\Entity\Customer\Customer
            sorting:
                createdAt: desc
            fields:
                id:
                    type: string
                    label: ID
                email:
                    type: string
                    label: Email
                createdAt:
                    type: datetime
                    label: Created At
            filters:
                search:
                    type: string
                    label: Search by email
                    options:
                        fields: [email]
            actions:
                main:
                    create:
                        type: create
                item:
                    update:
                        type: update
                    delete:
                        type: delete

Trong ví dụ trên:

  • driver: dùng Doctrine ORM để lấy data từ entity Customer.

  • fields: cột hiển thị trong grid.

  • filters: bộ lọc, VD: search theo email.

  • actions: các action có thể thực hiện (create, update, delete).

3. Cách render Grid trong Controller

Trong Sylius, grid không tự hiển thị mà ta cần gọi service sylius.grid.controller hoặc extend ResourceController.

Ví dụ:

use Sylius\Bundle\GridBundle\Controller\GridController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class CustomerController extends GridController
{
    public function indexAction(Request $request): Response
    {
        return $this->index($request, 'app_admin_customer'); // grid code
    }
}

4. Cách render Grid trong Twig

Trong template Twig, Sylius có helper:

{{ sylius_grid_render(grid) }}

Hoặc render riêng filter:

{{ sylius_grid_render_filter(grid, 'search') }}

5. Custom Grid

  • Thêm cột mới: khai báo trong fields.

  • Format dữ liệu: dùng type: twig và custom template.

  • Thêm filter nâng cao: có nhiều filter type như boolean, date, entity.

  • Row actions: ví dụ tạo action “View Profile” với type: link.

6. Tại sao nên dùng Grid

  • Tái sử dụng: Một grid có thể gọi ở nhiều nơi (backend, API).

  • Decoupled: không phụ thuộc chặt vào entity, chỉ cần data source.

  • Có thể extend để hiển thị data từ API, CSV, hay service khác (không nhất thiết Doctrine).

Tóm lại: Grid view trong Sylius là công cụ mạnh mẽ để build bảng dữ liệu quản trị. Nó gần giống như EasyAdmin datagrid hoặc Sonata Admin list view, nhưng lightweight và linh hoạt hơn, phù hợp với kiến trúc “resource” của Sylius.