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!
18.
Property Metadata
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
}
}
10 Comments
Hi,
I have a hierarchy of contents: News extends Content, Tag extends content.
Slug is defined on Abstract Content superclass.
The problem is that the identifier for the News is the {id} but the identifier for the Tag is {slug}; the Slug, common to both entity, is on Superclass.
Is there a way to customize ApiProperty(identifier=<<some function>>) so I can avoid to replicate slug property also on tag, only because identifier is different from news?
I have tried with ApiProperty(identifier=self::SLUG_AS_IDENTIFIER) , in superclass, but obviusly does not work cause self is related to masterclass, also in Tag class ... static::SLUG_AS_IDENTIFIER does not work..
Is possible? or I have to define $slug also on Tag?
Thanks
Hey Gianluca-F!
The only way I can think of is to hook into the "property metadata factory" system: this is the system that collects all of the API Platform metadata about each property. To do this, you would create a service that decorates https://github.com/api-platform/core/blob/2.7/src/Metadata/Property/Factory/PropertyMetadataFactoryInterface.php - kind of like we do for the resource metadata factory here - https://symfonycasts.com/screencast/api-platform-security/resource-metadata-factory - except that decoration is easier in Symfony now with the AsDecorator attribute.
Anyways, you would add your own property metadata factory decorator that, when called, would call the inner property metadata factory then, if the resource class is the class you want and the property is the one you want, change its metadata to set it as an identifier / not an identifier. Looking at one of the core decorators might help: https://github.com/api-platform/core/blob/2.7/src/Metadata/Property/Factory/AttributePropertyMetadataFactory.php
So, doable - but not in a super simple way.
Cheers!
hello, tried with old and the latest api platform versions, but somehow normalization context +groups does not help show fields for this DailyStats, really strange, for User and CheeseListings - works as expected, I use SF5.2
clearing cache didn't help https://github.com/api-plat...
Hey Mihail !
Hmmm. So you simply mean that, if you set
normalizationContext={"groups"={"daily-stats:read"}},and then put@Groups({"daily-stats:read"})above a property (exactly like we're doing in this video), that property isn't returned? Or are you doing something a bit different that we're doing here? It is possible that a bug was introduced in Symfony 5.2, but in that case, I can't think why it would affect this class, but not the entity classes.Cheers!
Hey, Ryan!
Definitely, I tried to rewrite this part several times, this happens only to the custom resource.
I checked on github that APIPlatform is still being updated to SF5.2 and the issue is present in `2.6.0-alpha.1` https://github.com/api-plat...
The version for code example to this tutorial for SF5.1 works as expected.
Hey Mihail!
Ah, interesting! What happens if you try Symfony 5.2 and API Platform 2.5? API Platform 2.5 does work Symfony 5.2 - the
^5.1in API Platform's composer.json allows 5.2. I would be curious if the bug lies in using Symfony 5.2 or API Platform 2.6 (my "instinct" is that it is in API Platform 2.6, but I could definitely be wrong). If we can figure this out, it would be worth creating an issue on either API Platform or Symfony - it smells like a bug.Cheers!
OMG I see the mistake
"dailystats:read"vs"daily-stats:read"Somehow it does not react to manual setting
normalizationContextgroups either in camelcase "dailyStats:read" or with dash "daily-stats:read", or "dummystats:read". But the data is displayed correctly with default groups"dailystats:read"`For class defined like below I see only
totalVisitors``/**
)
/ class DailyStats { /*
*/
public $date;
/**
*/
public $totalVisitors;
}
`
Hey Mihail!
Wait, I want to make sure I'm not getting confused. In the above code, where you have
"groups"={"dummystats:read", ONLY thetotalVisitorsproperty is returned... even though it only has the groupdailystats:read? Who/what is setting that group - do you have some "context builder" somewhere that's adding extra groups? I think I'm not seeing some detail...Cheers!
Hey Ryan,
Oh yeah, now I see my mistake, I'm still using the `AutoGroupResourceMetadataFactory::getDefaultGroups` from https://symfonycasts.com/sc...
While it has been deleted for the 3rd tutorial :)
Just disabled - everything works!
Whooops! Yea, sorry about that. I deleted it for the 3rd tutorial to help make "less" variables for people watching the tutorial to keep track of... but it had the opposite effect for you!
Happy you figured it out!
Cheers!
"Houston: no signs of life"
Start the conversation!