Skip to content

Commit 5a03858

Browse files
Add pagination info for custom data providers (#1194)
1 parent b324225 commit 5a03858

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

core/data-providers.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,54 @@ final class BlogPostItemDataProvider implements ItemDataProviderInterface, Restr
238238
}
239239
```
240240

241+
## Use Pagination in Custom Collection Data Provider
242+
243+
If you are implementing your own collection data provider, you might also want to support pagination. You can do
244+
this by returning a `ApiPlatform\Core\DataProvider\PaginatorInterface` instance.
245+
246+
API Platform provides a few paginators, e.g. `ApiPlatform\Core\DataProvider\ArrayPaginator` and
247+
`ApiPlatform\Core\DataProvider\TraversablePaginator`.
248+
See the [Pagination page](pagination.md) for more information on pagination.
249+
250+
You can access the paging information by injecting the `ApiPlatform\Core\DataProvider\Pagination` service, and
251+
using it within your data provider.
252+
253+
```php
254+
<?php
255+
// api/src/DataProvider/CustomCollectionDataProvider.php
256+
257+
declare(strict_types=1);
258+
259+
namespace App\DataProvider;
260+
261+
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
262+
use ApiPlatform\Core\DataProvider\Pagination;
263+
use ApiPlatform\Core\DataProvider\PaginatorInterface;
264+
use ApiPlatform\Core\DataProvider\TraversablePaginator;
265+
266+
final class CustomCollectionDataProvider implements CollectionDataProviderInterface
267+
{
268+
private Pagination $pagination;
269+
270+
public function __construct(Pagination $pagination)
271+
{
272+
$this->pagination = $pagination;
273+
}
274+
275+
public function getCollection(string $resourceClass, string $operationName = null, array $context = []): PaginatorInterface
276+
{
277+
$page = $this->pagination->getPage($context);
278+
$itemsPerPage = $this->pagination->getLimit($resourceClass, $operationName, $context);
279+
280+
$data = [/* results */];
281+
$results = new \ArrayIterator($data);
282+
$totalItems = count($data);
283+
284+
return new TraversablePaginator($results, $page, $itemsPerPage, $totalItems);
285+
}
286+
}
287+
```
288+
241289
## Community Data Providers
242290

243291
If you don't want to use the built-in Doctrine system, alternative approaches which offer an integration with API Platform exist.

core/pagination.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,3 +542,15 @@ class BookRepository extends ServiceEntityRepository
542542
}
543543
}
544544
```
545+
546+
## Pagination for Customer Data Providers
547+
548+
If you're using a customer Data Providers, and not the Doctrine ORM, ODM or
549+
ElasticSearch provides, then if you want your results to be paginated then
550+
you'll need to return an instance of a
551+
`\ApiPlatform\Core\DataProvider\PartialPaginatorInterface` or
552+
`\ApiPlatform\Core\DataProvider\PaginatorInterface`. A few existing classes are
553+
provided to get you started with these…
554+
555+
* `\ApiPlatform\Core\DataProvider\ArrayPaginator`
556+
* `\ApiPlatform\Core\DataProvider\TraversablePaginator`

0 commit comments

Comments
 (0)