This tutorial has a new version, check it out!

Symfony RESTful API: Course 1


What you'll be learning

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.

In Course 1, we'll start quick with:

  • Build your first functional POST endpoint
  • Decoding the Request body
  • Using forms to handle incoming data
  • Writing tests for your API
  • Handling data "seeding" for your tests
  • GET, PUT, DELETE and PATCH endpoints
  • Using the JMS Serializer

Your Guides

Ryan Weaver Leanna Pelham

Buy Access

Questions? Conversation?

  • 2019-02-08 Daniel Gustaw


    Your voting system is totally awesome.😍 Now I feel that I have influence on topics that you will presents.
    I have to tell all my friends 🧑👨👧🎃 to subscribe Symfony Cast and vote like me as soon as possible :D 😇

    Thank You!

  • 2019-02-08 Victor Bocharsky

    Hey Daniel,

    Awesome news for you! We've started working on ApiPlatform course! Well, it's on the planning stage yet, but I think it will be available in a month or two. And yeah, it's a *top* secret! So, shhhhhhh, don't tell anyone ;)

    P.S. And the correct link is: https://symfonycasts.uservo...


  • 2019-02-07 Daniel Gustaw

    This link shows: "This UserVoice instance does not exist.". I would be very happy if API platform course would be created.

  • 2019-01-20 joan Q

    Thanks a lot! Following it with Symfony4 is what I'm doing since I wrote the message, but with your answer I am more comfortable with my decision!

  • 2019-01-17 Diego Aguiar

    Hey joan Q

    Thank you for your honest feedback. We're working on a way to reduce this kind of problems (related to old dependencies) but it will take us some time to deliver it. What I recommend you in the meanwhile is to follow this course on Symfony4. it may cause you some troubles because the architecture changed but it's not that hard, and if you have problems, you can always leave us a message.
    Oh, and about the dependencies, I think you will have problems with Guzzle only, but you can install the latest version and adjust the code, the library didn't change too much (at least the functions we use in the tutorial)


  • 2019-01-15 joan Q

    I'm trying to follow this tutorial but I can't find a way to do it. My first option was to do it 'as it is', although I know it is very old. I thought "first I will learn how everything works and, later, I will update my knowledge to the new symfony versions". But soon I discovered that I couldn't do that because my system has PHP 7 and I can't go beyond the 3rd video if I don't make some updates in code and dependencies (reverting to PHP 5 is not an option!). I've been struggling for hours with several ways to do it with no success. I always end up with some problem related to unresolvable dependencies (as I am very far from an expert on this, I feel myself as I were crashing against a wall over and over). So, at this point, I'm wondering... what would you advise me to do?

  • 2018-11-24 Lukasz

    FOSRestBundle is not a good idea

  • 2018-09-20 Diego Aguiar

    Hey kaizoku

    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.


  • 2018-09-20 kaizoku

    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-11 Mario Brkic

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

  • 2018-09-11 Victor 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!


  • 2018-09-11 Mario Brkic

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

  • 2018-02-20 weaverryan

    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).


  • 2018-02-20 Hugo 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 ( ). 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 ).

    I hope you all enjoy, cya.

  • 2018-02-05 Victor 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.


  • 2018-02-05 Marcinho Mascarenhas

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

  • 2018-01-17 Paweł Chry


  • 2018-01-17 weaverryan

    Hey Paweł Chry!

    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.

    I hope that helps! Cheers!

  • 2018-01-16 Paweł Chry

    Do I need before Symfony RESTful 5-pack courses first go with these two courses?

  • 2017-04-14 Victor Bocharsky

    Hey Martin,

    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.


  • 2017-04-12 David Song

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

  • 2017-04-12 Martin 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?


  • 2017-04-11 weaverryan

    Hey David Song!

    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: You can get the code by downloading it, or I have a copy of it here:

    Let me know if that helps!

  • 2017-04-06 David 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():

    $testEnvMapper = Middleware::mapRequest(function (RequestInterface $request) {
    $path = $request->getRequestTarget();
    $modified_request = $request->withRequestTarget('/app_test.php'.$path);
    return $modified_request;
    self::$staticClient = new Client([
    'base_uri' => 'http://localhost:8000',
    'handler' => $handler,
    'http_errors' => false

    Here is the modified request from the captured history:
    string(3) "PUT"
    string(24) "/app_test.php/customer/2"
    object(GuzzleHttp\Psr7\Uri)#284 (7) {
    string(4) "http"
    string(0) ""
    string(9) "localhost"
    string(11) "/customer/2"
    string(0) ""
    string(0) ""

    As you can see "requestTarget" has been correctly modified, but the Uri Object's "path" field is still the original.
    All my tests use the guzzle client like this:

    $response = $this->client->put('customer/2', [
    'body' => json_encode($data)

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

  • 2017-02-27 Achilles Kaloeridis

    Thanks :)

  • 2017-02-27 Victor 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 ;)


  • 2017-02-26 Achilles 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??

  • 2017-01-19 Henri Tompodung

    It's work! Thanks Victor :)

  • 2017-01-18 Victor Bocharsky

    Hey Henri,

    Ah, looks like this issue was already solved - could you follow the thread to get it worked? Let me know if it helps.


  • 2017-01-18 Henri Tompodung

    Hi Ryan, i get an error when i want to install it..

    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

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

    Thx anyway..

  • 2016-06-18 weaverryan

    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 :).


  • 2016-06-16 Filipe Moraes

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

  • 2016-02-29 weaverryan

    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:

    public function someAction()
    $html = $this->renderView('mypage/some_template.html.twig', array('twigVar' => 'twigVal'));

    // return pure HTML
    return new Response($html);
    // or return HTML inside JSON, under an "html" key (but this key could be anything).
    return new JsonResponse(array('html' => $html));


  • 2016-02-29 ibn mrabet achraf

    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-25 Tac Tacelosky

    +1 for a tutorial that uses FOSRestBundle and Angular.

  • 2015-06-22 Victor 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-22 weaverryan

    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.


  • 2015-06-22 Victor 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-21 weaverryan

    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).


  • 2015-05-21 Nick

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

  • 2015-05-21 weaverryan

    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.


  • 2015-05-21 Nick

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

  • 2015-05-16 weaverryan


    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.


  • 2015-05-15 prasath_s

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

  • 2015-05-13 prasath_s

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

  • 2015-04-23 Ghulam Kibria Ali

    Can't wait for this course! :D

  • 2015-03-25 twomartoe

    Eagerly awaiting this too, in the meantime, check this out

  • 2015-03-19 weaverryan

    Hey Nick!

    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.


  • 2015-03-19 Nick

    Any estimate? Will you use angular and fosrestbundle for this?