dwalczyk / paginator-bundle
Symfony 分页Bundle
1.1.1
2024-01-31 20:37 UTC
Requires
- php: ^8.2
- doctrine/orm: ^2.11
- symfony/framework-bundle: ^6.0|^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.13
- phpstan/phpstan: ^1.10
- spaze/phpstan-disallowed-calls: ^2.16
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); }
自定义数据加载器
- 创建一个实现 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 } }
- 用你的新数据加载器替换默认的数据加载器(doctrine QueryBuilder)。
# config/services.yaml services: ... DWalczyk\Paginator\DataLoaderInterface: '@App\Service\SamplePaginatorDataLoader'
- 完成!