lucas-gerard/range-pagination

此包已弃用且不再维护。未建议替代包。

一个用于处理带有Range HTTP头部的分页的Symfony 4包

安装: 841

依赖项: 0

建议者: 0

安全: 0

星级: 2

观察者: 1

分支: 0

开放问题: 0

类型:symfony-bundle

v1.0.1 2018-04-27 09:37 UTC

This package is not auto-updated.

Last update: 2023-02-23 11:30:30 UTC


README

一个用于处理带有Range HTTP头部的分页的Symfony 4包

Composer安装

只需运行以下命令来安装该包

composer require lucas-gerard/range-pagination

并在services.yaml中注册EventSubscriber

[...]
services:
    LucasGerard\RangePagination\EventSubscriber\:
        resource: '../vendor/lucas-gerard/range-pagination/EventSubscriber'

这样就可以了!

使用方法

头部格式

此包基本上通过使用这些格式解析Range HTTP头部,向你的控制器提供一个Range对象

Range: items=0-9
// Or
Range: items=0-
// Or
Range: items=0-*

访问Range实例

你现在可以在你的控制器中访问一个Range对象,这里有两种可能性

/**
 * Inject a range in your controller
 */
public function indexAction(Range $range)
{
    // You can use $range here
}

/**
 * Access it from your request object
 */
public function indexAction(Request $request)
{
    $range = $request->attributes->get('range');
    // Then use $range
}

获取项目

如果你使用findAll()方法,请将其切换为findBy()并设置第三个和第四个参数

$items = $repository->findAll()
// Switch it to this :
$items = $repository->findBy([], null, $range->getLimit(), $range->getStart());

如果你使用Doctrine查询访问数据

$qb = [...]; // A QueryBuilder
$qb->getQuery()
    ->setFirstResult($range->getStart())
    ->setMaxResults($range->getLimit());

$items = $qb->getResult(); // Returns a collection containing the items within the range

构建响应

一切都应该相当简单,只需设置JsonResponse的内容、响应代码和Content-Range头部将自动处理。

查看Range类以查看所有可用方法。

待办事项

  • 测试
  • 配置选项(启用/禁用响应代码和头部等)

许可证

此包受MIT许可证的约束。有关完整版权信息,请参阅随源代码一起分发的LICENSE文件。

致谢

Lucas Gerard : lucas.gerard.web@gmail.com

请随意发送建议和/或pull requests!