Skip to content

Add new articles about REST APIs #5260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wouterj opened this issue May 15, 2015 · 19 comments
Closed

Add new articles about REST APIs #5260

wouterj opened this issue May 15, 2015 · 19 comments
Labels
actionable Clear and specific issues ready for anyone to take them. Waiting feedback

Comments

@wouterj
Copy link
Member

wouterj commented May 15, 2015

We currently completely ignore this topic, while it is a quite populair topic these days. #4022 started discussing this, but it didn't have a lot of progress.

@wouterj wouterj added actionable Clear and specific issues ready for anyone to take them. needs comments labels May 15, 2015
@94noni
Copy link
Contributor

94noni commented May 29, 2015

+1 for this
and maybe a simple demonstration in the symfony-demo app could be useful (symfony/demo#69 (comment))

@marioaojeda
Copy link

+1!

@bocharsky-bw
Copy link
Contributor

It will be useful 👍

@wouterj
Copy link
Member Author

wouterj commented Jul 23, 2015

As I don't have a lot of REST experience, I would be happy if people could indicate what exactly will be usefull. What are the problems you're facing with REST in Symfony? Where did you find help? What would you like to see in the official docs?

@timglabisch
Copy link
Contributor

i think we should split this to a bunch of different articles.
rest is quite complex and there are so much topics and so much different ways to implement them :)

@bocharsky-bw
Copy link
Contributor

Yes, you're right, it's a quite complex.
I think @weaverryan could propose some REST problems and give advice about structure in generally, if he has a time.

@weaverryan
Copy link
Member

Best advice: start small - the previous effort was huge. A short tutorial showing the following would go along way:

A) Controller route methods
B) Grabbing and decoding JSON from the request body
C) Returning JSON directly (no serializer)
D) Using the serializer to return JSON
E) Controlling status codes
F) Controlling the request format

It is indeed such a big complex thing, that I think it's paralyzing. Whenever we give an example anywhere, we should think about using a template sometimes, and making something an "API" endpoint in another.

@ghost
Copy link

ghost commented Nov 9, 2015

+1

2 similar comments
@dudemelo
Copy link

dudemelo commented Nov 9, 2015

👍

@fdyckhoff
Copy link

+1

@weaverryan
Copy link
Member

This is a huge, and not-well-defined task. In general, we need to be more API-centric in the docs, but I don't think anyone is going to take the time to create a huge new section (unless I or a few core contribs find time to do it).

For those that are looking here, I worked/am working on http://knpuniversity.com/screencast/symfony-rest. The scripts are free and the code-blocks below are expandable so you can get a full context. If there are parts of this that people would specifically find useful in the core docs, I'd be happy to help bring those over. Start with small, executable goals :).

@javiereguiluz
Copy link
Member

I'm copying below a comment from @michaelperrin about this in a duplicated issue that we closed.


With the rise of APIs and service-oriented architecture, it would be great to have a starting point on how to build REST APIs with Symfony and some third-party bundles making it easier.
The current documentation is still very MVC-centric, although consuming APIs with JavaScript frameworks(for instance) gets very common. There are many tutorials and many ways to build REST APIs, but I think that newcomers would be glad to find such a starting point.

A step-by-step example would be appropriate, with an API built upon a simple entity (with a least one relation), with the main operations (create, update, delete, show, search) being impletend. The following subjects could be mentioned:

  • Quick reminder of HTTP verbs
  • HATEOAS
  • API documentation (NelmioApiBundle / Swagger)

Should the API be built with Dunglas Core Api Bundle or FOSRestBundle? I like the pragmatic approach of Dunglas Bundle and the strong focus on standards, but I have no experience with it yet, and it may be out of the scope of the main Symfony documentation.

We should either way choose one very pragmatic approach, so that users don't get confused and overwhelmed by the many possibilities. Other possibilities could however be mentioned.


I'm also asking for help in the Symfony Slack to see if some truly expert in Symfony + APIs can share with us the modern best practices about that so we can start writing some articles. Thanks!

@javiereguiluz javiereguiluz changed the title Add new cookbook recipes about REST Add new articles about REST APIs Mar 7, 2017
@gnugat
Copy link

gnugat commented Mar 7, 2017

javiereguiluz:
Thank you all! So, those of you that spend most of your time writing APIs with Symfony, could you please add a comment in this issue #5260 with a bullet list of your "best practices". There's no need to explain things in detail for now. Just a bullet list of things to do and not to do. And if you know excellent and modern references (code, doc, blog posts) about this, please share them too. Thanks! (edited)

Here's my humble contribution, a bullet list of the steps we follow to create a new endpoint:

  1. create a Controller class
  2. configure its routing
  3. register the controller as a service

We might need to create some event listeners (to populate $request->request when receiving JSON content, or to convert exceptions to responses).

The endpoint's logic is then up to us, it doesn't have to be done in a "Symfony" way. For example we can:

  • extract Request parameters and put them in a class that validates them
  • pass the class to a handler that will call services to do the actual logic
  • define our services as interfaces, and then create implementations to integrate them with third party libraries

Here's an example of such a controller:

<?php
// src/AppBundle/Controller/Api/FortuneController.php

namespace AppBundle\Controller\Api;

use AppBundle\Service\SubmitNewFortune;
use AppBundle\Service\SubmitNewFortuneHandler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class FortuneController
{
    private $submitNewFortuneHandler;

    public function __construct(SubmitNewFortuneHandler $submitNewFortuneHandler)
    {
        $this->submitNewFortuneHandler = $submitNewFortuneHandler;
    }

    public function submit(Request $request): Response
    {
        $submitNewFortune = new SubmitNewFortune(
            $request->request->get('content')
        );
        $this->submitNewFortuneHandler->handle($submitNewFortune);

        return new Response('', 201);
    }
}

@adamquaile
Copy link

As Symfony generally does not enforce certain techniques or conventions, is it worth mentioning that REST is not the only way to build APIs. Perhaps a note about how to choose whether REST is the right choice when starting a new build..?

@fbourigault
Copy link
Contributor

fbourigault commented Mar 7, 2017

Does it worth to write a large documentation for building an API with Symfony? Or does we just need a smaller one that just explain some basic things and redirect users to Api Platform/FOSRestBundle?

I would chose the 2nd option as Api Platform works out of the box and require less work than the simplest hand made REST API.

@linaori
Copy link
Contributor

linaori commented Mar 7, 2017

Personally I'm not a fan of FOSRestBundle at all. So I would love to see ways of doing so without this bundle.

@Simperfit
Copy link
Contributor

@fbourigault I think we should build a little bit of documentation about what you will need to build an API, basic HTTP principle and maybe people don't want to use a Bundle and to build it alone.

For those who talked about a symfony-demo app for building APIs, that's what ApiPlatform Standard Edition is doing.

@magarzon
Copy link

magarzon commented Mar 7, 2017

After years developing APIs REST with Symfony, from scratch, using the Form Component, then FOSRestBundle and now API Platform, I'd recommend to use API platform, but IMHO it has two "problems": hard learning curve for beginners and too much "Hydra centered" (Hydra is not suitable for all APIs, specially private ones used for M2M communication)

Anyway, the documentation should have the following topics:

  • If you like DIY, don't use Form Component. Use Validator+Serializer component
  • Authentication: How to use JWT Tokens
  • Documentation: How to use Swager or Nelmio
  • Routing: This depends on the bundle/framework used, but probably some reference to best practices naming routes in API REST
  • Response: Serialization
  • Errors: HTTP error codes to return in each case, and "standard" error responses, like API Platform and application/vnd.error+json
  • Pagination, filtering, ...: Content-Range header, Link headers,...

@javiereguiluz
Copy link
Member

I propose to close this issues because in all these years, nobody took the lead to contribute these docs. The good news is that things have changed a lot during this time. We now have ApiPlatform as a great project to create APIs in Symfony apps. The documentation of that project is the way to fix this issue. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
actionable Clear and specific issues ready for anyone to take them. Waiting feedback
Projects
None yet
Development

No branches or pull requests