Chapters
48 Chapters
|
5:13:28
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!
Subscribe to download the code!
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
22.
Custom Paginator
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
This tutorial also works great with API Platform 2.6.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": ">=8.2",
"ext-ctype": "*",
"ext-iconv": "*",
"api-platform/core": "^2.1", // v2.5.10
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/annotations": "^1.0", // 1.12.1
"doctrine/doctrine-bundle": "^2.0", // 2.1.2
"doctrine/doctrine-migrations-bundle": "^3.0", // 3.0.2
"doctrine/orm": "^2.4.5", // 2.8.2
"nelmio/cors-bundle": "^2.1", // 2.1.0
"nesbot/carbon": "^2.17", // 2.39.1
"phpdocumentor/reflection-docblock": "^3.0 || ^4.0 || ^5.0", // 5.2.2
"ramsey/uuid-doctrine": "^1.6", // 1.6.0
"symfony/asset": "5.1.*", // v5.1.5
"symfony/console": "5.1.*", // v5.1.5
"symfony/debug-bundle": "5.1.*", // v5.1.5
"symfony/dotenv": "5.1.*", // v5.1.5
"symfony/expression-language": "5.1.*", // v5.1.5
"symfony/flex": "^1.1", // v1.21.6
"symfony/framework-bundle": "5.1.*", // v5.1.5
"symfony/http-client": "5.1.*", // v5.1.5
"symfony/monolog-bundle": "^3.4", // v3.5.0
"symfony/security-bundle": "5.1.*", // v5.1.5
"symfony/twig-bundle": "5.1.*", // v5.1.5
"symfony/validator": "5.1.*", // v5.1.5
"symfony/webpack-encore-bundle": "^1.6", // v1.8.0
"symfony/yaml": "5.1.*" // v5.1.5
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.3", // 3.3.2
"symfony/browser-kit": "5.1.*", // v5.1.5
"symfony/css-selector": "5.1.*", // v5.1.5
"symfony/maker-bundle": "^1.11", // v1.23.0
"symfony/phpunit-bridge": "5.1.*", // v5.1.5
"symfony/stopwatch": "5.1.*", // v5.1.5
"symfony/twig-bundle": "5.1.*", // v5.1.5
"symfony/web-profiler-bundle": "5.1.*", // v5.1.5
"zenstruck/foundry": "^1.1" // v1.8.0
}
}
5 Comments
Hey there !
Is it possible to extend Paginator somehow with custom properties? I want it to include not only hydra:member & hydra:totalItems but a custom property. Imagine querying transactions - what I want is to receive 20 transactions per page and calculate the total value of all transactions that match the query
Cheers!
Hey Denrolya!
That's an interesting question! I've never done it before, but I believe you need a custom normalizer. When you have a paginated response, what really happens (assuming you're using Doctrine ORM) is that this class - https://github.com/api-plat... - is your "data" and then that is run through the serializer. Other normalizers - for example https://github.com/api-plat... - already look for this class (well, specifically, they usually look for PaginatorInterface) and add the properties you see. So, if you create a custom normalizer and target the Paginator class, you should be able to add new properties :).
Cheers!
Hi there,
I am implementing a custom controller with a custom action following the Api Platform Documentation (https://api-platform.com/do... for the pagination but the hydra:member of the JSON LD response is completely empty. The other fields of the pagination are correct but this one no.
Can somebody help me?
Thank you!
Raul
Hey Raul M.!
I don't have a lot of experience with custom controllers & actions. The fact that the other pagination fields are correct, but
hydra:memberis empty is super odd. My best advice for debugging is to try to use the paginator in your custom controller with some debug code to make sure it works. So, something like:We're checking to make sure that you can correctly iterate over the paginator and see the results. It's just a guess to try to figure out what's going wrong :).
Cheers!
Hi Ryan,
I tried to implement your custom paginator and it works, thanks!
Now I'am in hurry with my project but I will try the debbug during the next weeks because it is so strange.
Cheerio!
"Houston: no signs of life"
Start the conversation!