dwalczyk/paginator-bundle

Symfony 分页Bundle

1.1.1 2024-01-31 20:37 UTC

This package is auto-updated.

Last update: 2024-09-30 01:41:06 UTC


README

Symfony 框架的分页Bundle。

安装

Composer

composer require dwalczyk/paginator-bundle

将Bundle添加到应用中

// config/bundles.php
DWalczyk\Paginator\PaginatorBundle::class => ['all' => true]

使用方法

基本使用

默认支持Doctrine QueryBuilder。

use DWalczyk\Paginator\PaginatorInterface;

#[Route('/users')]
public function __invoke(PaginatorInterface $paginator)
{
    $res = $paginator->paginate(
        $target = $this->getEm()->createQueryBuilder()->select('u')->from(User::class, 'u'), 
        $page = 1, 
        $itemsPerPage = 30
    );
    
    dump($res);
}

自定义数据加载器

  1. 创建一个实现 DWalczyk\Paginator\DataLoaderInterface 的类
<?php

namespace App\Service;

use DWalczyk\Paginator\DataLoaderInterface;

class SamplePaginatorDataLoader implements DataLoaderInterface
{
    public function loadItems(mixed $target, int $offset, int $limit): array
    {
        // some logic here
    }

    public function loadTotalCount(mixed $target): int
    {
        // some logic here
    }
}
  1. 用你的新数据加载器替换默认的数据加载器(doctrine QueryBuilder)。
# config/services.yaml
services:
  
  ...
  
  DWalczyk\Paginator\DataLoaderInterface: '@App\Service\SamplePaginatorDataLoader'
  1. 完成!