Skip to content

Commit 3388c36

Browse files
toby-griffithsalanpoulain
authored andcommitted
Add section on pagination for custom Data Prviders
1 parent 8c20a05 commit 3388c36

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

core/data-providers.md

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ final class BlogPostItemDataProvider implements ItemDataProviderInterface, Seria
166166

167167
## Injecting Extensions (Pagination, Filter, EagerLoading etc.)
168168

169-
API Platform provides a vendor specific extensions that you can reuse in your custom DataProvider.
169+
API Platform provides a few extensions that you can reuse in your custom DataProvider.
170170
Note that there are a few kinds of extensions which are detailed in [their own chapter of the documentation](extensions.md).
171171
Because extensions are tagged services, you can use the [injection of tagged services](https://symfony.com/blog/new-in-symfony-3-4-simpler-injection-of-tagged-services):
172172

@@ -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.

0 commit comments

Comments
 (0)