Symfony RESTful API: Course 1

What you'll be learning

Tip: This course is built on Symfony 2, but most of the concepts apply just fine to Symfony 3.

In this series, we get to work: by building the same API in the
REST series, but leveraging
all the amazing tools and libraries of the Symfony framework. If terminology
like representations, resources, and hypermedia are new to you, start
with the REST series and
then come back. And get ready to get your (API) mind blown.

Questions? Conversation?

You can watch our courses about REST API's, it will help you understanding how to develop your own API, how to test it, and some best practices, but we do not talk about "API Platform" on those courses.

Cheers!

2018-09-20kaizoku

Hi Victor, I need to start a new project with an API. If I plan to go with API Platform which tutorial should I follow here on SFcast ? Does the Symfony RESTful API: Course will help or it's unrelated ? Thx.

2018-09-11Mario Brkic

Thanks, that's good to know. Will be looking out for it.

2018-09-11Victor Bocharsky

Hey Mario,

We do want to release this course, you can see it here: https://knpuniversity.userv... - but unfortunately we do not have any estimations about its release date yet, sorry. Thanks for your patience!

Cheers!

2018-09-11Mario Brkic

Is there a plan to release a course on using API Platform to develop a REST (or GraphQL) API?

2018-02-20weaverryan

Very cool Hugo do Carmo! Thanks for sharing this - awesome! And yea... hopefully we'll get that PR merged soon - I'm still trying to decide about that *last* part - if we should add a system to also be able to load .env.ENVIRONMENT files (e.g. .env.test).

Cheers!

2018-02-20Hugo do Carmo

Hi there, if you're interested to see how this course would look in the new Symfony 4 (with Flex) architecture, you can take a look in my repository ( https://github.com/spelcast... ). I've also used Guzzle 6 to test the api controller.

I had to refactor a few things to get it working, some of them were big changes and were not that intuitive, and others were very easy to accomplish.

I had a hard time to separate the test environment, fortunately, there is an open PR by weaverryan that should fix or at least mitigate the problem (I recommend you to visit the link to understand the problem https://github.com/symfony/... ).

I hope you all enjoy, cya.

2018-02-05Victor Bocharsky

Hey Marcinho,

Good question! Well, it won't be the next tutorial we're released, but we're definitely going to talk about API with Symfony 4.

Cheers!

2018-02-05Marcinho Mascarenhas

HelloSuggestion. Could update this course and launch one with symfony 4

You do not need to go through those other REST courses before going through this course :). Those courses give deeper explanations of REST concepts, but I refer to them in this screencast. So, if there is some REST idea that is still not clear in this tutorial, you can quickly look at the other tutorials *if* you want deeper explanation.

Actually, it's completely up to you... or probably your API users. Think about your customers, do they want to send another request for fetching addresses or would like to get them in the customer request. My point here is that if API users need all this info at once, I mean fetching a customer info without addresses is pointless for them, so they always will send another request to get missing addresses, then better to include addresses into the /api/customers/123 endpoint response. But if API users need addresses really rare (they are just interested in basic customer info without addresses), then better to make another endpoint like /api/customers/123/addresses to allow fetching this info only when needed. That's it, I really don't see any significant disadvantages in both approaches.

Cheers!

2017-04-12David Song

Thanks Ryan! withUri() instead of withRequestTarget()!

2017-04-12Martin Bittner

How do you manage an entity with a collection of object?

Say a customer entity may have multiple addresses. Would you have /api/customers/123 (123 being an id), display all customer info and its addresses? Or you would have /api/customers/123/addresses

Same when you create/update, would you accept json (or xml or other) with customer info and its addresses and then code logic in the controller that would instanciate a customer object, and then loop through addresses to create an address entity, link it to the customer and save everything?

Sorry for the slow reply - somehow your message landed in SPAM at first >:( - but we rescued it :). In episode 4 of this tutorial, we actually upgraded to Guzzle 6 and made this change. So, I'm happy to say that you can see some working code there: http://knpuniversity.com/sc.... You can get the code by downloading it, or I have a copy of it here: https://gist.github.com/wea...

Let me know if that helps!

2017-04-06David Song

Hey Ryan! How are you doing? In Chapter 13, in ApiTestCase's setUpBeforeClass(), you are using Guzzle 5's emitter->on('before') to rewrite the target Path of the request (prepending the app_test.php to every request). I've had to modify your code since that is no longer supported in Guzzle 6. So far, I've got it correctly rewriting the "requestTarget" in the Request object but the client still goes to the unmodified target, here is the relevant excerpt from my setUpBeforeClass():

And they are all hitting the unmodified target. Any insights? I've scoured resources online to no avail.

2017-02-27Achilles Kaloeridis

Thanks :)

2017-02-27Victor Bocharsky

Hey Achilles,

Yes, you could! It's a matter of taste. But both do the same things. Actually, Symfony client is even simpler because it's already pre-installed in Symfony SE, i.e. you don't need to require it manually with Composer. So just use the one you like the most ;)

Cheers!

2017-02-26Achilles Kaloeridis

Hey Ryan, great stuff!!! I have some questions. Couldn't we use Symfony\Bundle\FrameworkBundle\Client instead of Guzzle? Like the Symfony documentation says in the functional tests section. Don't those tools supposed to do the same thing? Is there any advantage of using Guzzle instead of Symfony Client??

[Doctrine\Common\Annotations\AnnotationException] You have to enable opcache.load_comments=1 or zend_optimizerplus.load_comments=1.

Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the post-install-cmd event terminated with an exception

[RuntimeException] An error occurred when executing the "'cache:clear --no-warmup'" command.

Thx anyway..

2016-06-18weaverryan

Hey Filipe!

You're absolutely right - and we move them into the BaseController eventually (once we actually need them in multiple places - actually it's something we do "offline" between episodes 4 and 5). Of course, many people - like you - will already know this will need to happen, so you can put it directly into BaseController :).

Cheers!

2016-06-16Filipe Moraes

Why the methods "processForm" , "getErrorsFromForm" and "throwApiProblemValidationException" are not within the class "BaseController"? These methods are common to all controllers.

2016-02-29weaverryan

Hi there!

You absolutely *can* just return HTML in your API instead of JSON - it's often an easy shortcut to make updating your HTML page with AJAX really easy. Here's how it would look:

hey guys,how to get a twig.html page as response in api Controller for example: I get information from the user then i want to display a html page containing those information !

2015-08-25Michael Tacelosky

+1 for a tutorial that uses FOSRestBundle and Angular.

2015-06-22Victor Bocharsky

There is a lot of information about REST on KnpU. I think I better to start from scratch and I'm almost sure there I discover a useful information for me too. I always open something new for myself with your screencasts. They're a very cognitive and helps to improve the understanding of the subject area! Thanks!

2015-06-22weaverryan

Hey Bocharsky!

You can start right with this one :). If you're quite new to REST/API stuff and really want to go deep, I'd watch the other one - though in this screencast, we occasionally say "if you want more info, go to this URL to watch part of the REST series" - to help out with this.

Cheers!

2015-06-22Victor Bocharsky

Hey Ryan! Do I need to finished "RESTful APIs in the Real World" courses first to start this course for Symfony? Does this courses closely connected?

2015-05-21weaverryan

I think FOSRestBundle is a bit like FOSUserBundle - you can use it, or you can fairly easily build that same stuff yourself and get more flexibility. No one option is best - using the bundle gets you setup faster of course :).

BazingaHateoasBundle is *wonderful*, and we will use it later in the series. The question here is, do you really need links in your API? They're all the rage, but it depends on who you're building your API for (e.g. if your API is for your JS frontend, maybe you don't need links, or need very few).

Cheers!

2015-05-21Nick

Thanks for reply, for a real application though is FosRestBundle andBazingaHateoasBundle recommended or you think without those you can create a real life professional application without issues and clean enough?

2015-05-21weaverryan

Hey Nick!

Good question! The FosRestBundle is basically a set of various tools - like helping you decode the JSON request, helping serialize the Form errors, doing content-type negotiation and adding a view layer. These are all peripheral tools - and when you get building, you find that you need probably only a few of those. I don't use it in the screencast, though I could have for JSON deserialization and a few other minor things.

Cheers!

2015-05-21Nick

Hey! Why not use FosRestBundle? Or maybe this will be in episode 2?

2015-05-16weaverryan

Hey!

On Angular, no plan yet - but I'm going to see if anything angular-specific might be nice to cover as an addendum to this series. I mean, you'll be rocking a very solid API after this series, but if there are any tweaks needed to play even nicer with Angular, maybe we can add something for that after.

The good news is that we're releasing this series right now - expect more next week.

Cheers!

2015-05-15prasath_s

Ye Angular would be great! Any news on that front? And also when you are planning to publish the series? Cheers:)

2015-05-13prasath_s

Cant wait either:) Hope that more of this series is soon to be released!!:)

We haven't started on this yet, but it's up next - so it's still at least several weeks away. YES, it will use FOSRestBundle. I'm not sure about angular yet - but I think it might be a good idea - after all, that's what a lot of people are using to consume those APIs.