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!
23.
Pagination Context
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
}
}
6 Comments
I'm not sure if this issue specific for my project, but without passing "$context" to "getPagination" I couldn't get actual page, so I did this way:
$this->pagination->getPagination($resourceClass, $operationName, $context);Hey Rafal,
Hm, interesting... in the latest version of ApiPlatform I see that $context is optional, see https://github.com/api-plat... . Probably it's required in your version, you can double-check that file in your vendor/ directory. But IIRC it should not be required anyway. It might be you misunderstand the error message?
Cheers!
Hey Victor,
Great tutorial - BTW :)
I'm using version 2.5.7. When you follow methods path you'll end up here: https://github.com/api-plat... - so page number is actually taken from the context.
Hey Rafal,
Thank you!
Ah, I think I see... You call getPagination() where the context is an optional arg, but then this method calls getPage() which in turn calls getParameterFromContext() where the $context is a required arg... though since $context arg has a default arg set to an empty array upstream - it should not throw any errors, but yeah, $context is data are required to work properly I think.
Cheers!
Hello ! I was wondering if it was possible to do something more generic for the Paginator. Currently, we need to create a Paginator class for each custom resource and implement its own logic for each.
If we changed this DailyStatsPaginator to a more generic class, which can be used for each custom resource, that would be great!
In this sense, I tried to modify the code of this class somewhat.
The actual result looks like this: https://gist.github.com/bas...
And now the current DailyStatsProvider class : https://gist.github.com/bas...
Currently, it works. But it is quite sensitive.
The best (I don't know if this has an impact on the performance side) would be to pass all the results each time (like with your KnpPaginatorBundle), and let the DataPaginator take care of the results to insert for the current page.
In this case, we are forced to pass as a parameter the results for the current page, as well as the total result count, while all this could be calculated by the DataPaginator, but in this case we would have to modify the behavior of the StatsHelper class .
Some improvements are therefore to be reviewed I think
Hey Kiuega
That's a good idea but also a bit hard to do. Want I recommend is to have at least 3 different cases where you want to reuse the paginator, so then, you can start looking for a way to parameterize all the things that varies and make the functionality (the paginator) more generic
The
StatsHelperclass is just a convenient class to delegate some internal work, you should feel free to modify it as much as requiered for your application.I don't know if it makes sense to you. Cheers!
"Houston: no signs of life"
Start the conversation!