@@ -238,6 +238,54 @@ final class BlogPostItemDataProvider implements ItemDataProviderInterface, Restr
238
238
}
239
239
` ` `
240
240
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\C ore\D ataProvider\T raversablePaginator` .
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\D ataProvider;
260
+
261
+ use ApiPlatform\C ore\D ataProvider\C ollectionDataProviderInterface;
262
+ use ApiPlatform\C ore\D ataProvider\P agination;
263
+ use ApiPlatform\C ore\D ataProvider\P aginatorInterface;
264
+ use ApiPlatform\C ore\D ataProvider\T raversablePaginator;
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 \A rrayIterator($data);
282
+ $totalItems = count($data);
283
+
284
+ return new TraversablePaginator($results, $page, $itemsPerPage, $totalItems);
285
+ }
286
+ }
287
+ ` ` `
288
+
241
289
# # Community Data Providers
242
290
243
291
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