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!
46.
UUID as a API Identifier
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
}
}
17 Comments
Hi All!
I'm creating an API , and want to use UUID's. Everything seems to work, except for PUT requests. I always get this error:
"hydra:description": "Cannot assign Symfony\\Component\\Uid\\UuidV4 to property App\\Entity\\Subscription::$id of type ?int",The entity is pretty straigt forward:
I'm really losing my mind on this, and google or chatgpt are not my friend :(
Is there any genius that sees what I am missing?
Symfony version: 6.4.2
API Platform version: 3.1.14
Thanks a lot, all help is really appreciated.
EDIT: sorry, I seem to struggle with the code block, I can't manage to put it in one codeblock ...
Hey @Ben01
It seems you have your API resource set up correctly, I cannot spot the error. Does it only fail on PUT requests?
How are you making the request?
Cheers!
Hi, Thanks for your reply!
It turns out it was a problem with the version of API Platform. I updated Symfony from 6.3 to 6.4, but didn't update api-platform.
I updated to the latest API-platform version, and now it works again!
Great news!
Hi all! I have created an API using Symfony 6 and API Platform 3. For the first entities, everything went fine but I have an error now when I am doing a GET collection request for User entity (
api/users) :So why this happens and how to fix it ?
Here is an excerpt from my User entity :
Thanks in advance!
Hey Marko!
Weird! The error comes from this class - https://github.com/api-platform/core/blob/main/src/Symfony/Routing/IriConverter.php - unfortunately that exact error wording is used 3 times in that file, so it's not clear exactly which "bad path" you're hitting. But it's likely you're hitting one of these 2 errors - https://github.com/api-platform/core/blob/9b4b58ca0113a2645b58a116fe9e4bf200df8aa3/src/Symfony/Routing/IriConverter.php#L171-L191
In both cases, there is a DIFFERENT error that occurs, and then it throws the error you're seeing. However, Symfony DOES display the original error as well. If you open the profiler for this failed request and go to the "Exception" tab, you should see something near the top that says something like "Exceptions 1/2". If you scroll down, eventually you'll see the OTHER exception. It may give you a better clue about what is going wrong. For example, if you left
usernameblank, the system would blow up because it would call->getUsername()during this process, but theusernameproperty would be uninitialized. That's just one possible problem :)Cheers!
Hi @weaverryan. Thanks a lot for your answer. I managed to fixed this bug with the following code:
Niiice! Thanks for sharing that :)
Thank you for your excellent videos!
I'm trying to set up a table ("users") with a composite key - a (Ramsey) uuid_binary and an integer - and then use that as a foreign key in another table ("companies"). I think I have it mostly working, but I have a few issues / questions, please.
1) Is there a meaningful way for me to define the item "get" end point - something like /users/{userUuid}/something_else/{anotherId} ?
I don't think it's strictly a sub-resource, but possibly composite keys don't really fit for this kind of end point in REST? I can't work out how (if possible) to get the API website to display an entry box for "anotherId" in addition to the "userUuid" one.
2) I've got the "companies" table get request displaying the "userUuid" string and "anotherId" integer value on its jsonld output. I guess, partially based on question 1), that there's no reasonable way to output an IRI instead - since it would be combining 2 fields into 1, potentially?
3) Related to 2), I guess there's also no reasonable way I could return a "users" record, rather than just the key values, since that would, again, be associated with 2 fields on "companies"?
PHP 8.1, Symfony 6.1, API Platform 2.8, doctrine-bundle 2.7, MySQL 8.0.26
I'm using uuid_binary in this case, because it's a Type 4 UUID. I'm aware of the uuid_binary_ordered_time version for Type 1 UUID - some of which I will also be using in my project.
(I have an example I could include, but it's pretty big, even after I minimised most of functionality... I'm not sure it's beneficial?)
Oh, I guess the attributions are useful, in case I'm doing something obviously foolish with those. When I "get" /companies/1 I have an IRI for "address", but values for "insertedBy" and "another".
Entity/Users.php
Entity/Companies.php
Entity/Addresses.php
Debugging into the code, I've got a bit further, I think. If I do this, I think it mostly solves question 1 - I get valid IRIs with two parameters for "users". I can curl for the entries in jsonld format, but a web browser gives me the full HMTL interface when I use the /users/uuid/{userUuid}/another/{anotherId} end point with a .jsonld or .json extension.
EDIT: If I remove the 'path' => ... line, the default endpoint, /users/{userUuid}/{anotherId}.jsonld DOES work correctly in a web browser. Weird.
Entity/Users.php
Hey Paul-L!
WOOOH. Ok, so you're doing some seriously complex stuff - and it looks like you've made some good headway since your original question (the team was waiting for me to read and reply to your comment).
In general, back to (1), I've actually never created a URL like this - I typically tell people that life is much easier if you let the URL be a filter - e.g. /users/{userUuuid}?another={anotherId}. However, if you've got it working - nice job!
After all of your progress, are you still struggling with any part of this?
Cheers!
Thanks weaverryan.
I think I understand 1) sufficiently now - at least enough to make it work for me - though fighting with this has also helped me find a way to refactor out the composite key in this case anyway.
And I'm pretty sure that the answers to 2) and 3) are that there isn't really a meaningful way to replace two lines on the results from GET /companies/1 (userUuid and anotherId) with a single IRI or a record from the user table - where would it live in the hierarchy?
I suspect the problem I encountered with paths using slugs might be an API platform bug (or user misuse). It doesn't work as I expect with non-composite keys either. If I add
'path'=>'/companies/id/{id}'to the itemOperations "get" entry on Companies, http://127.0.0.1:8000/companies/id/1.jsonld in my web browser takes me to the API Platform OpenAPI web interface but this curl command returns the ld+json entry I'd expect:Hey Paul-L!
Sorry for the slow reply - just back from some vacation :).
The ability to add the .jsonld extension and see that in your browser is kind of a "bonus" feature just to make life easier when debugging (because using a browser is nice). This is "powered" by the fact that the routes built by API Platform look like this:
/api/cheeses/{id}.{_format}. In your case, for your custom "path", I bet if you added.{_format}to the end, you would get the result you expect. So, it's not a misuse on your part - though you do need a little bit more if you want that nice browser behavior.Cheers!
Hey guys! Loved this tutorial.
But I sill miss some hardcore examples. I have a legacy application and I had to create an API to talk to it. So no Doctrine at all. Not even for user authentication!
Then I really got in trouble when I have to deal with sub resources. Mainly many to many relationships. For instance: A user can be in multiple locations. So there's an endpoint to user, another for locations and I had to create a third one to add the user to a location.
I hacked my way with custom `collectionOperations` + controllers and `OpenApiFactory` decorator to fix the documentation. But I would love some best practices on that.
Thanks!
for reference: There's a stackoverflow question about this:
https://stackoverflow.com/a...
cool tutorial!
Hey Fox C.
Good work on StackOverflow! And thanks for mentioning us!
Cheers!
"Houston: no signs of life"
Start the conversation!