定制 chamber-orchestra/pagination-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

chamber-orchestra/pagination-bundle

Composer 安装命令:

composer require chamber-orchestra/pagination-bundle

包简介

Symfony pagination bundle with support for arrays, Doctrine ORM repositories, queries, Twig rendering, and filter building

README 文档

README

PHP Composer codecov PHPStan Latest Stable Version License PHP 8.5 Symfony 8

ChamberOrchestra Pagination Bundle

Symfony bundle for paginating arrays, Doctrine ORM repositories, and Doctrine ORM queries. Ships with a type-based pagination factory, built-in paginators, and Twig rendering helpers.

Features

  • Type-based paginationPaginationType (basic next/prev), RangeType (numbered page links with surrounding range), and ExtendedPaginationType (next/prev with total counts)
  • Cursor-based paginationCursorType for keyset pagination using a single cursor value with direction derived from QueryBuilder orderBy
  • Auto-resolved cursor fields — ULID entities automatically resolve cursor_field and cursor_getter from Doctrine metadata
  • Built-in paginatorsArrayPaginator, EntityRepositoryPaginator, QueryPaginator, and CursorQueryPaginator
  • Extended pagination — optional total element count and page count computation for API metadata
  • Twig integrationrender_pagination() function with an overridable sliding template
  • Repository traitPaginationEntityRepositoryTrait adds list / listBy helpers to Doctrine repositories
  • Autowiring support — all services are auto-configured and tagged via Symfony DI

Installation

composer require chamber-orchestra/pagination-bundle

If you are not using Symfony Flex, register the bundle manually:

// config/bundles.php
return [
    ChamberOrchestra\PaginationBundle\ChamberOrchestraPaginationBundle::class => ['all' => true],
];

Optional dependencies

Package Purpose
doctrine/orm + doctrine/doctrine-bundle Doctrine ORM pagination
symfony/uid Auto-resolution of ULID cursor fields
twig/twig Twig pagination rendering

Usage

Array pagination

use ChamberOrchestra\PaginationBundle\Paging;
use ChamberOrchestra\PaginationBundle\Pagination\PaginationFactory;

final class BookController
{
    public function __construct(
        private Paging $paging,
        private PaginationFactory $paginationFactory,
    ) {
    }

    public function index(): array
    {
        $pagination = $this->paginationFactory->create('range', [
            'page' => 1,
            'limit' => 10,
            'extended' => true,
        ]);

        $items = ['a', 'b', 'c'];
        $result = $this->paging->paginate($items, $pagination);

        return [
            'data' => $result,
            'meta' => $pagination->createView()->vars,
        ];
    }
}

Doctrine EntityRepository pagination

use ChamberOrchestra\PaginationBundle\Paging;
use ChamberOrchestra\PaginationBundle\Pagination\PaginationFactory;
use Doctrine\ORM\EntityRepository;

public function list(EntityRepository $repository, Paging $paging, PaginationFactory $factory): array
{
    $pagination = $factory->create('range', [
        'page' => 1,
        'limit' => 20,
        'extended' => true,
    ]);

    $items = $paging->paginate($repository, $pagination, [
        'criteria' => ['status' => 'active'],
        'orderBy' => ['id' => 'ASC'],
    ]);

    return iterator_to_array($items);
}

Doctrine Query/QueryBuilder pagination

use ChamberOrchestra\PaginationBundle\Paging;
use ChamberOrchestra\PaginationBundle\Pagination\PaginationFactory;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Book;

public function list(EntityManagerInterface $em, Paging $paging, PaginationFactory $factory): array
{
    $query = $em->createQueryBuilder()
        ->select('b')
        ->from(Book::class, 'b')
        ->orderBy('b.id', 'ASC')
        ->getQuery();

    $pagination = $factory->create('range', [
        'page' => 2,
        'limit' => 10,
        'extended' => true,
    ]);

    $items = $paging->paginate($query, $pagination);

    return iterator_to_array($items);
}

Cursor-based pagination

Cursor pagination uses a single cursor value instead of page numbers, providing stable results and efficient queries for large datasets. The pagination direction (forward/backward) is derived from the QueryBuilder's orderBy clause.

use ChamberOrchestra\PaginationBundle\Paging;
use ChamberOrchestra\PaginationBundle\Pagination\PaginationFactory;
use ChamberOrchestra\PaginationBundle\Pagination\Type\CursorType;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Book;

public function list(
    Request $request,
    EntityManagerInterface $em,
    Paging $paging,
    PaginationFactory $factory,
): array {
    $pagination = $factory->create(CursorType::class, [
        'cursor' => $request->query->get('cursor'),
        'limit' => 20,
    ]);

    $qb = $em->createQueryBuilder()
        ->select('b')
        ->from(Book::class, 'b')
        ->orderBy('b.id', 'ASC');

    $result = $paging->paginate($qb, $pagination, [
        'cursor_field' => 'b.id',
        'cursor_getter' => static fn (Book $book): mixed => $book->getId(),
    ]);

    return [
        'data' => $result,
        'meta' => $pagination->createView()->vars,
        // {
        //   "cursor": "42",
        //   "limit": 20,
        //   "next": "62",
        //   "previous": "43"
        // }
    ];
}

Auto-resolved cursor fields (ULID entities) — for entities with a ULID primary key, cursor_field and cursor_getter are auto-resolved from Doctrine metadata. No options needed:

// Entity with #[ORM\Column(type: 'ulid')] identifier — just pass the QueryBuilder
$result = $paging->paginate($qb, $pagination);

This is handled by CursorFieldPaging, a decorator around Paging that is automatically registered when doctrine/orm is available. It inspects the QueryBuilder's root entity metadata and resolves the ULID identifier field and getter.

Reading cursor from request automatically — when the cursor option is omitted, CursorType reads it from the cursor request query parameter:

// GET /books?cursor=42
$pagination = $factory->create(CursorType::class, [
    'limit' => 20,
    // 'cursor' is read from ?cursor= automatically
]);

Cursor presence indicates page availabilitygetNextCursor() returns null when there is no next page, and a cursor string when there is. Same for getPreviousCursor().

Twig rendering (optional)

{{ render_pagination(pagination_view) }}

Default templates are in src/Resources/views/ and can be overridden in your application.

Pagination types

Type Description View vars
pagination Basic next/previous navigation current, startPage, previous, next
range Numbered page links with configurable range current, pagesCount, elementsCount, startPage, endPage, previous, next, pages, pageParameter, limit
ExtendedPaginationType Next/previous with total counts current, previous, next, pagesCount, elementsCount
CursorType Cursor-based (keyset) pagination cursor, limit, next, previous

The pagination, range, and ExtendedPaginationType types accept page, limit (default 12), page_parameter, and extended options. The range type additionally accepts page_range (default 8).

The CursorType accepts cursor (?string), and limit (int, default 12). It requires a QueryBuilder target with an orderBy clause, and the cursor_field + cursor_getter (\Closure) paginator options (auto-resolved for ULID entities).

Development

composer install
composer test        # PHPUnit
composer analyse     # PHPStan (level max)
composer cs-check    # PHP-CS-Fixer (dry-run)

License

MIT

chamber-orchestra/pagination-bundle 适用场景与选型建议

chamber-orchestra/pagination-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.99k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「orm」 「symfony」 「paging」 「twig」 「api」 「paginator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 chamber-orchestra/pagination-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 chamber-orchestra/pagination-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 chamber-orchestra/pagination-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.99k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 20
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-02