You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Examples for custom controllers
Many are missing examples how to use pagination with custom controllers.
This could help.
* Update pagination.md
* Update core/pagination.md
Co-Authored-By: martinharder <[email protected]>
* Update core/pagination.md
Co-Authored-By: martinharder <[email protected]>
* Use method in registry instead of service
The method is moved to the registry.
Controller extends AbstractController.
Copy file name to clipboardExpand all lines: core/pagination.md
+125Lines changed: 125 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -345,3 +345,128 @@ class Book
345
345
```
346
346
347
347
Please note that this parameter will always be forced to false when the resource have composite keys due to a [bug in doctrine](https://github.com/doctrine/doctrine2/issues/2910)
348
+
349
+
## Custom Controller Action
350
+
351
+
In case you're using a custom controller action make sure you return the `Paginator` object to get the full hydra response with `hydra:view` (which contains information about first, last, next and previous page). The following examples show how to handle it within a repository method. The controller needs to pass through the page number. You will need to use the Doctrine Paginator and pass it to the API Platform Paginator.
352
+
353
+
First example:
354
+
355
+
```php
356
+
<?php
357
+
358
+
//src/Repository/BookRepository.php
359
+
360
+
namespace App\Repository;
361
+
362
+
use App\Entity\Book;
363
+
use App\Entity\User;
364
+
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
365
+
use Doctrine\ORM\EntityManagerInterface;
366
+
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
367
+
use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator;
368
+
use ApiPlatform\Core\Bridge\Doctrine\Orm\Paginator;
369
+
use Doctrine\Common\Collections\Criteria;
370
+
371
+
class BookRepository extends ServiceEntityRepository
372
+
{
373
+
const ITEMS_PER_PAGE = 20;
374
+
375
+
private $tokenStorage;
376
+
377
+
public function __construct(
378
+
RegistryInterface $registry,
379
+
TokenStorageInterface $tokenStorage
380
+
) {
381
+
$this->tokenStorage = $tokenStorage;
382
+
parent::__construct($registry, Book::class);
383
+
}
384
+
385
+
public function getBooksByFavoriteAuthor(int $page = 1): Paginator
The service need to use the proper repository method.
435
+
You can also use the Query object inside the repository method and pass it to the Paginator instead of passing the QueryBuilder and using Criteria. Second Example:
436
+
437
+
```php
438
+
<?php
439
+
440
+
//src/Repository/BookRepository.php
441
+
442
+
namespace App\Repository;
443
+
444
+
// use...
445
+
446
+
class BookRepository extends ServiceEntityRepository
447
+
{
448
+
// constant, variables and constructor...
449
+
450
+
public function getBooksByFavoriteAuthor(int $page = 1): Paginator
0 commit comments