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!
16.
Completely Custom Resource
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
}
}
15 Comments
If you were creating this tutorial on custom resources for api-platform version 2.7 would you change anything significant? I'm asking since I'm currently trying to upgrade an (fully working) app from php7.4/symfony4.4/api-platform2.6 to php8.1/symfony5.4/api-platform2.7 and all my routes work except for the custom routes where I get a 404 not found exception. Following these videos I think I have everything necessary in the various places (and I also used the automatic upgrade script provided by api-platform) but I'm left scratching my head as to why the routes can't be found. Many thanks!
A little more info: changing
to
makes the difference between the route being found or not, with the annotation version being the working one and the attribute one being the non-working version. In order for this to work I need to have
metadata_backward_compatibility_layer: trueset in api_platform.yamlHey Alexander!
Sorry for the slow reply - holidays! To be honest, I'm not sure yet. I'm going to be working on the API Platform 3 tutorial over the next few weeks, and 2.7 and 3.0 are, I believe, effectively identical from a feature point of view.
In theory, going from 2.6 -> 2.7 should not cause a problem, as 2.7 doesn't break backwards-compatibility. But, API Platform is complex, so it's possible some minor, accidental changes occurred. I was about to mention
metadata_backward_compatibility_layer, but it seems that you found that setting! That setting is fine for now, but you'll need to set it tofalsebefore upgrading to API Platform 3. What's missing to allow you to set it tofalseand still see your route? Unfortunately, I can't answer that yet :/.Sorry I can't give a better answer - my knowledge isn't "there" yet in this case :).
Cheers!
Sorry I forgot to delete my comment, I solved my issue. Don't ask how because I have no idea, but now it works and before it didn't :) Hope you enjoyed the holidays!
Haha, no worries! Glad it's working :D
hallo,
i try to find a way to use LazyString as at 1:51,
so I create a property fakeName in user (the property is not in the serializazion group for the collection)
than in postLaod event i have done this
and than i create the method itself
but when i run http://localhost/api/users
all the dumps appears
so maybe i m not using it correctly?
can you suggest me a better way?
I am finding a couple of issues when applying this to graphql. I will report where I believe appropriate.
It seems that naming an Entity, or at least an ApiResource, with a name in plural is not a good idea as far as graphql is concerned.
When I enabled graphql I could not find the collection query for DailyStats. After I checked all else, I attempted to change the name of the Entity to DailyStat and voila! the collection query was where it shoud be. Of course, I changed all else from DailyStats to DailyStat.
API-Platform by default seem to assume that ApiResource name are singular and automatically pluralizes it to get the name of the collection query. As it got queries with the same name, it dropped one of them. Or so I believe.
Hey Bernard A.!
> I am finding a couple of issues when applying this to graphql. I will report where I believe appropriate.
Happy to have them :).
> When I enabled graphql I could not find the collection query for DailyStats. After I checked all else, I attempted to change the name of the Entity to DailyStat and voila! the collection query was where it shoud be. Of course, I changed all else from DailyStats to DailyStat.
Ah, bad pluralization! That's my fault. In English, we don't even think of "statistics" as plural, but it absolutely is. DailyStat would, indeed, be a better name.
Cheers!
Hey, everyone!
It seems that, at least in API Platform 2.6.5, adding
@ApiResource()is not enough, since it requires to explicitly mark the identifier for the resource. So, you'll get an error like this:No identifier defined in "App\Entity\DailyStats". You should add #[\ApiPlatform\Core\Annotation\ApiProperty(identifier: true)]" on the property identifying the resource.By adding the following
@ApiProperty(identifier=true)to the property that you want to uniquely identify the resource, the problem is solved.In this case I added to the
dateproperty, since we're talking about <b>daily</b> stats:`/**
*/
public $date;
`
Hope it helps!
Cheers!
Hey André P.
You're right, defining the ApiPlatform identifier is required when you're not working on an Entity, in other words, when working with a DTO
Cheers!
Thank you, Diego.
Just realized that in the next chapter this ends up being dealt with, but it may help someone who gets stuck with that error on this one.
Cheers!
How can I Unit Test the API in a stand alone Symfony Bundle?
Hey M_Holstein !
That's an excellent question! And the answer has a lot more to do with "how do I functionally test a bundle" than it does with API Platform. Here is an example from another tutorial - https://symfonycasts.com/sc... - it does not include API Platform (so you will need to add that as bundle to the kernel... and there may be some other changes), but I hope it will point you in the right direction :).
Cheers!
Hello.
I have added a completely new Custom Resource, which gets data from an external service. Now I want to show this resource on another Entity based on its @id. How can I do that? I tried creating a new CustomResource($id) object but it doesn't go through the CustomDataProvider I wrote for it.
Thanks
Hi mega!
That's an interesting question! You're right about the data provider: the data provider is only execute for whatever the top level component is. So, for example, if the user goes to /api/products/5, then the data provider for this "Product" entity is executed. Suppose you want that to return a new "customResource" property that contains your CustomResource. If you add that property to Product, all API Platform will do is call
$product->getCustomResource()to get that property (no data provider for CustomResource is involved, as you saw).The way to solve this is to add a custom data provider for your Product entity. This would probably decorate the normal Doctrine data provider, but then it would populate the customResource property on Product. So, it's a bit laborious - but that's the idea.
Let me know if that helps!
Cheers!
"Houston: no signs of life"
Start the conversation!