Chapters
20 Chapters
|
1:32:53
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: >=5.3.3
Subscribe to download the code!Compatible PHP versions: >=5.3.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!
02.
Finish POST with a Form
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 an older version of Symfony. The concepts of REST are still valid, but I recommend using API Platform in new Symfony apps.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": ">=5.3.3",
"symfony/symfony": "2.6.*", // v2.6.11
"doctrine/orm": "~2.2,>=2.2.3,<2.5", // v2.4.7
"doctrine/dbal": "<2.5", // v2.4.4
"doctrine/doctrine-bundle": "~1.2", // v1.4.0
"twig/extensions": "~1.0", // v1.2.0
"symfony/assetic-bundle": "~2.3", // v2.6.1
"symfony/swiftmailer-bundle": "~2.3", // v2.3.8
"symfony/monolog-bundle": "~2.4", // v2.7.1
"sensio/distribution-bundle": "~3.0,>=3.0.12", // v3.0.21
"sensio/framework-extra-bundle": "~3.0,>=3.0.2", // v3.0.7
"incenteev/composer-parameter-handler": "~2.0", // v2.1.0
"hautelook/alice-bundle": "0.2.*", // 0.2
"jms/serializer-bundle": "0.13.*" // 0.13.0
},
"require-dev": {
"sensio/generator-bundle": "~2.3", // v2.5.3
"behat/behat": "~3.0", // v3.0.15
"behat/mink-extension": "~2.0.1", // v2.0.1
"behat/mink-goutte-driver": "~1.1.0", // v1.1.0
"behat/mink-selenium2-driver": "~1.2.0", // v1.2.0
"phpunit/phpunit": "~4.6.0" // 4.6.4
}
}
13 Comments
Hello,
When I write in ProgrammerController.php
public function newAction(Request $request)
{
$body = $request->getContent();
return new Response($body);
}
ide highlights getContent();
and when I type testing.php in the cmd I get:
> php testing.php
HTTP/1.1 500 Internal Server Error
Host: localhost:8000
Connection: close
Cache-Control: no-cache
Date: Mon, 14 Aug 2017 03:29:33 GMT
X-Debug-Token: 1943b6
X-Debug-Token-Link: /_profiler/1943b6
Content-Type: text/html; charset=UTF-8
What could be the reason the error?
How can I to fix this problem?
Hey Nina
I believe you chosed the wrong type of Request, make sure you are using "Symfony\Component\HttpFoundation\Request"
if that's not the case, let me know :)
Cheers!
Hi,
Thanks for you tutorail it are always amazing.
I have a little question, I try to use Postman to make my call for "api/programmes" with in body form-data (nickname in key and Bob for value etc for the others data) I have nothing in $request->getContent()
Edited:
I found how to send data I need to use raw but why can I use form-data ?
If I use Guzzle I have no pb.
Do you know why?
Thanks again.
Hey Greg,
That's because Postman sends form-data/raw with a different headers. For the raw type "Content-Type" header equals to "text/plain" so the Symfony's Request object do not parse it itself, you need to call $request->getContent() to get request content. But for the form-data type "Content-Type" header of request is "multipart/form-data", so Request object parse content and you put data on the "$request" property for POST data and on the "$query" property for GET request. So if you send a POST request with "form-data" type via Postman - then you can get already parsed data in app with $request->request->get('your-sent-key-here').
P.S. use "dump($request->request->all())" to dump all sent POST data in your app.
Cheers!
Hello guys and gals from KnpU!
My question is, since the Programmer resource has a relation attached to the user who created it. I suppose that a resource /programmers from a user 1 is different from a resource /programmers from a user 2, although they have the same URL. Would it be better to explicit this relation in the URI? e.g: app/users/{slug}/programmers.
What are the pros and cons when using this approach? I heard that it its good for using http caching.
Regards.
Hey Felipe!
Ah, GREAT question. Basically, yes! If you follow the rules of REST, then really, if you want to return a collection of all of the programmers from a specific user, you should have something like
/users/{slug}/programmers, not/programmers. Caching is one reason. But more broadly, in in REST, each URL is an address to a specific resource... and I shouldn't be able to suddenly get back a different resource just by sending a different authentication token. As a by product of that rule, you can use HTTP caching more easily :). It's also a bit more explicit!In the real world, it does seem like most API's follow this practice, but it's not an absolute rule. For example, if you use Facebook's API, I believe you use the URL
/meto fetch your user profile, and/me/photosfor my photos. On the bright side, "cheating" like this makes it even easier for your clients :).Cheers!
Cool! Thanks Ryan.
For those of us who choose to purchase just this tutorial, I think providing the full picture (e.g. the Programmer model) would make this easier to replicate.
Hey Caro!
I totally want you to be able to replicate this :). On this page, you should see a "Download" link in the upper right corner, with a "Course Code" option in it. This will download a .tar file. Inside, there is a "start" directory (how the code looks at the beginning of this tutorial) and a "finish" directory (how the code looks at the end of the tutorial).
Does this help? Or is there still something missing?
Cheers!
yes it did, thank you so much!
Hi there,
Everything looks normal except when I compare my output to the expected output shown at 2:00 in the video I don't see the JSON output. The headers look exactly the same as the expected output:
----------------------------------------------------
HTTP/1.1 200 OK
Host: localhost:8000
Connection: close
X-Powered-By: PHP/5.6.27
Cache-Control: no-cache
Date: Wed, 11 Jan 2017 09:19:18 GMT
Content-Type: text/html; charset=UTF-8
X-Debug-Token: 7a9b8d
X-Debug-Token-Link: /_profiler/7a9b8d
-------------------------------------------------
The source for testing.php and ProgrammerController.php is an exact copy also. There is even an exact number of lines after the header, just no JSON line. Is there anything I should check, maybe one of the dependencies, or something I may have missed?
Actually no need to answer this one I worked it out, it was just a case of mistaken identity - got lost in my own folder structure.
Hey Dan ,
I'm glad you figured it out by yourself so quickly!
Cheers!
"Houston: no signs of life"
Start the conversation!