20.
Weird Endpoint: Command: Power-Up a Programmer
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.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
14 Comments
REST for me felt always like waste of time thinking about correct urls, status codes. As you showed also - there are situations where it does not fit in REST rules anyway. I remember long time ago I was using only GET and POST and whatever url felt good for me. And it was enough. No http codes. And I had no problems.
Hey Lijana Z.!
I think knowing what the "rules" are can help us normalize our APIs and make them all "work" a little more consistently. But yea, you can take this to an extreme - and that's exactly what I want people to not worry about. Not sure what the correct status code or HTTP verb is for an endpoint? Spend 5 seconds thinking about it, then choose your first choice and move on ;).
So, we're thinking alike!
Cheers!
Will there be a REST Course 6?
Right now, no - I don't see enough topics that we still haven't covered. But, if you have some topics that are still troubling you, then you're probably not the only one, and there might be a course 6 :)
Oh, except that we *may* do a course on documentation - that is one topic that we haven't covered yet.
The only problem I'm having with all this code right now is that I can't change the URL structure. If I wanted to use singular resource names, so "api/programmer" instead of "api/programmers" I'd be in trouble... I'd have to change all the tests and all the annotations in the controllers :(
Hey Johan,
But it's a good practice to hardcode URLs in tests. It's a bad idea to use router to generate URLs in tests. Imagine, you've accidentally changed some URL - then some tests will fail and show you the problem. of course you can move duplicated URL prefix in a private property, but probably it just worse test readability: tests should be well readable. And btw, URLs change very rare, i'd say API URLs never change ;)
Cheers!
Ye you're right. I checked the symfony docs and they say the same thing. Thanks :)
But what about we put @Route("/api/programmers") as a class annotation in the ProgrammerController and create routes relative to that route instead? I think it would be good :)
Thanks Victor
Yeah, I'm definitely +1 for it! We always try to do that if it possible - it reduces misprints. But here we left it as is to reduce complexity and do not produce questions: some parts of the class is hidden due to our dynamic code blocks.
Cheers!
That makes sense, thanks! :)
I need help to upload.
------WebKitFormBoundaryUFCyBVhvYZjTos7t
Content-Disposition: form-data; name="avatarImageFile"; filename="cadastro_consultas.jpg"
Content-Type: image/jpeg
���JFIFHH���ExifMM*bj
....
object(Symfony\Component\HttpFoundation\File\UploadedFile)[15]
private 'test' => boolean false
private 'originalName' => string 'cadastro_membros.jpg' (length=20)
private 'mimeType' => string 'image/jpeg' (length=10)
private 'size' => int 276654
private 'error' => int 0
private 'pathName' (SplFileInfo) => string 'C:\Windows\Temp\phpD540.tmp' (length=27)
private 'fileName' (SplFileInfo) => string 'phpD540.tmp' (length=11)
`In update resource with files to upload, showld i use POST instead PUT or PATCH????
Hey Carlos,
If you'd like to use PUT / PATCH methods, then you have to put the file's content in the body of your request I think, otherwise use POST to upload files. Here's a bit more details on how to upload a file using PUT: http://php.net/manual/en/fe...
Cheers!
Hi, thanks for this course. Shouldn't in real life the ProgrammerController->newAction better be available for anonymous? I you change this only by annotations. You get stuck with $this->getUser() which can't be used anonymously.
Hey Till,
Yes, the "ProgrammerController::newAction()" shouldn't be available for anonymous users, otherwise we will have a problem, since in "Programmer::setUser()" has the "public function setUser(User $user)" signature, i.e. we don't allow passing null here. However, "@Security("is_granted('ROLE_USER')")" annotation for the "ProgrammerController" class ensure that we always has a user in this controller, so if user is null - you can't get these end points.
Cheers!
"Houston: no signs of life"
Start the conversation!