Chapters
26 Chapters
|
2:18:26
|
This course is archived!
This tutorial uses a deprecated micro-framework called Silex. The fundamentals of REST are still ?valid, but the code we use can't be used in a real application.
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!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
03.
Serializer Annotations
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!
This tutorial uses a deprecated micro-framework called Silex. The fundamentals of REST are still ?valid, but the code we use can't be used in a real application.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"silex/silex": "~1.0", // v1.3.2
"symfony/twig-bridge": "~2.1", // v2.7.3
"symfony/security": "~2.4", // v2.7.3
"doctrine/dbal": "^2.5.4", // v2.5.4
"monolog/monolog": "~1.7.0", // 1.7.0
"symfony/validator": "~2.4", // v2.7.3
"symfony/expression-language": "~2.4", // v2.7.3
"jms/serializer": "~0.16", // 0.16.0
"willdurand/hateoas": "~2.3" // v2.3.0
},
"require-dev": {
"behat/mink": "~1.5", // v1.5.0
"behat/mink-goutte-driver": "~1.0.9", // v1.0.9
"behat/mink-selenium2-driver": "~1.1.1", // v1.1.1
"behat/behat": "~2.5", // v2.5.5
"behat/mink-extension": "~1.2.0", // v1.2.0
"phpunit/phpunit": "~5.7.0", // 5.7.27
"guzzle/guzzle": "~3.7" // v3.9.3
}
}
3 Comments
Hey Ryan, I am trying that my responses do not return integers as strings, i.e "id": 3, instead of "id": "3". I have used the @Type("integer") option of the JMSSerializer but apparently it has no effect in the output. Do you have any idea of how can this be achieved?
Hey Joan!
Ok, here's what's going on. If you don't specify a type, then the JSON will represent what the value is. So if the value is a string "5", you'll get "5" in the JSON. If it's a PHP integer 5, you'll get 5 without quotes. The reason you're always seeing quotes is just due to a shortcoming in the "ORM" that I built for this tutorial - it always fetches values from the DB as strings and sets them. In other words, if you dump a Programmer object after querying, you should see that the "integer" values are actually PHP strings. You should see a difference if you add something like the following to the bottom of the BaseRepository::createObjectFromData() function:
This causes true PHP numbers to be set, which removes the quotes from the JSON. Now the Type annotation also relates to this, but not in a way that looks super concrete to me. For example, if I set a true number, but mark that field as @Serializer\Type("string"), I see quotes in the JSON. But if I do the opposite - set a string on the object but mark the field as an "integer" - then I still see a string. It's a bit odd.
That's a long way of saying that the serializer should be creating JSON that represents what you have in PHP. and in this case, my ORM is always giving string values.
I hope that helps!
Thanks Ryan. I am not using your createObjectFromData() and I can't recall why, but the fix works for my own crappy version. Thanks :-)
"Houston: no signs of life"
Start the conversation!