Chapters
27 Chapters
|
2:54:11
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: ^7.1.3
Subscribe to download the code!Compatible PHP versions: ^7.1.3
-
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!
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 works great for Symfony 5 and API Platform 2.5/2.6.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.1.3",
"ext-ctype": "*",
"ext-iconv": "*",
"api-platform/core": "^2.1", // v2.4.3
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/annotations": "^1.0", // 1.10.2
"doctrine/doctrine-bundle": "^1.6", // 1.11.2
"doctrine/doctrine-migrations-bundle": "^2.0", // v2.0.0
"doctrine/orm": "^2.4.5", // v2.7.2
"nelmio/cors-bundle": "^1.5", // 1.5.5
"nesbot/carbon": "^2.17", // 2.19.2
"phpdocumentor/reflection-docblock": "^3.0 || ^4.0", // 4.3.1
"symfony/asset": "4.2.*|4.3.*|4.4.*", // v4.3.11
"symfony/console": "4.2.*", // v4.2.12
"symfony/dotenv": "4.2.*", // v4.2.12
"symfony/expression-language": "4.2.*|4.3.*|4.4.*", // v4.3.11
"symfony/flex": "^1.1", // v1.21.6
"symfony/framework-bundle": "4.2.*", // v4.2.12
"symfony/security-bundle": "4.2.*|4.3.*", // v4.3.3
"symfony/twig-bundle": "4.2.*|4.3.*", // v4.2.12
"symfony/validator": "4.2.*|4.3.*", // v4.3.11
"symfony/yaml": "4.2.*" // v4.2.12
},
"require-dev": {
"symfony/maker-bundle": "^1.11", // v1.11.6
"symfony/stopwatch": "4.2.*|4.3.*", // v4.2.9
"symfony/web-profiler-bundle": "4.2.*|4.3.*" // v4.2.9
}
}
13 Comments
2:40 We can do it more fancy way, using symfony/string component. https://symfony.com/doc/cur...
Ohh the String component, you're right, it would be super fancy to do it that way :)
Thanks for sharing it. Cheers!
Is it possible to specify properties[] on a POST request to control the data that is returned after success? A developer tried by POSTing the required data in the body and using the ?properties[] syntax, but instead of returning the fields he wanted, it seemed to filter out any data he had sent (that wasn't included in the properties array), making the POST payload invalid. Is that expected?
Hey AO!
THAT is really interesting. I'm not sure if it's expected, but I do see (now) that this would happen. The PropertyFilter works by modifying the (de)normalization context - https://github.com/api-platform/core/blob/master/src/Serializer/Filter/PropertyFilter.php#L43 - and it is called both at the start of the request during read/deserialization AND later during serialization. Basically, you only want it to happen during deserialization, which totally makes sense to me :p. Unfortunately, that doesn't look possible currently - the filter isn't set up for this. You could request this as an option on the PropertyFilter (that would make sense to me), but it doesn't work that way currently.
One option might be to sub-class PropertyFilter, use that in your annotation, and override apply(). I've never done this before, but I can't think of why it wouldn't work. Then, in your overridden apply, check the
$normalizationargument and ONLY call the parent apply() method if$normalizationis true.I'm not 100% sure this will work, but it's worth a shot ;).
Cheers!
Thanks for the reply, super useful! I ended up adding a POST normalization group that only sends ID back for now but I might take a look at that solution out of work hours :D
Awesome! Thanks for sharing your solution - it seems simple enough at least :).
Cheers!
Hi, Please how can I implement a filter that will filter my returned resources based on the available collections. For example the below returned data shows a collection property named 'exam'. I want the filter to return only the one with exam IRI property of "/api/exams/4". Thank you.
Hey Frederick E.!
Excellent question! Indeed, we didn't talk or think about how it might look to filter for a specific item (e.g. Group) based on the value/existence of some collection property. I just played with this, and fortunately, I think it's quite easy. I would:
1) On your Group entity, add a SearchFilter:
2) That's it! You should now see an option in Swagger to filter by this. You'll enter the IRI into the box. So, to only return "Groups" that have the exam /api/exams/4, it would look like
https://localhost:9022/api/users?exam=%2Fapi%2Fgroups%2F4- where the%2fthings are URL-encoded slashes.Let me know if this helps!
Cheers!
Awesome!!! Thank you weaverryan that helped....
Hi, when creating the getShortDescription() method, you indicate it "will return a nullable string, in case description isn't set yet", but in the code you didn't add the "?" for the return type of the method, right before "string". I wanted to update it myself but when I arrived in the GitHub repository, I just saw "[[[ code('5bacd98cdf') ]]]" and didn't know where to find this code. :/ It's not a very big issue, but it's worth fixing I think :p
Hey Ajie62!
You're right! Very nice catch! The video is right - the code is wrong. Thanks for trying to fix it... unfortunately (as you discovered!) this is one part of our process that is not contributor-friendly (I wish it were, but it's not). However, we're going to update that code block and add the nullable ? :)
Cheers!
I'm glad I can help at least a little.... :)
FYI, code block was fixed in https://github.com/SymfonyC...
Thank you for reporting it!
Cheers!
"Houston: no signs of life"
Start the conversation!