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!
28.
Custom Filter apply()
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
}
}
7 Comments
Passing info from Filter to DataProvider as below seems unnecessary.
$context[self::FROM_FILTER_CONTEXT] = $fromDate;
The same information is available on the Context on the DataProvider ( dump below):
See the key "filters" below:
Hey Bernard A.!
Ah, excellent catch! Yes, it looks like you could grab that and do all the processing you want. However, if you wanted to do any "cleaning up" of the filter (it's minor, but we convert it from a string to a DateTimeImmutable), you could still do that in your filter and pass *that* forward.
Cheers!
Also wondering why you chose to use a setter for setting the date on
DailyStatsPaginatorand making it non-nullable, when you could both make it nullable and use it as a constructor argument instead. I do understand that it doesn't make any difference in the end, but just wondering what the reason for this inconsistency might be. :)Yo Adeoweb!
Hmm, fair question :). I think in either case, the property would need to be nullable because the "from date" is optional: it's only present the query parameter is present: https://symfonycasts.com/screencast/api-platform-extending/apply-filter#codeblock-10bf80c57d. So then - unless I'm forgetting a detail - since it needs to nullable either way, it comes down to a pure preference. The setter is a bit more expressive than adding a 4th constructor argument... however you could also use named parameters in the constructor if you wanted the
new DailyStatsPaginator()code to be a bit more readable. So, yea, just coming down to preference here.Cheers!
I just noticed while debugging that at least in Api Platform 2.6, the
$this->pagination->getPagination()call inDailyStatsProviderline 28 actually needs a third argument –$context. Without it,$pageis always 1.Hey Adeoweb!
Ah, good catch! I bet that's a change in API Platform - I'll check it out and we'll add a note if needed (it sounds like it is needed).
Cheers!
Thanks for your replies. It seems like this was changed while preparing to release 2.4.0, in this commit.
"Houston: no signs of life"
Start the conversation!