Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Build REST APIs with Symfony2

Build REST APIs with Symfony2

Learn by example how to build a REST API using Symfony2 and third-party libraries. We will talk about routing, forms, serialization, testing, HTTP clients and documentation, we will also focus on how to write elegant and decoupled code.

Eduardo Oliveira

March 08, 2014
Tweet

More Decks by Eduardo Oliveira

Other Decks in Programming

Transcript

  1. What we are going to talk about? • HTTP API

    • Serialization • Documentation • Forms • Testing • HTTP Clients • API Design
  2. HTTP is a request/response protocol • Request ◦ URI and

    HTTP method ◦ Protocol version ◦ Entity (headers and body) • Response ◦ Status code ◦ Entity (headers and body)
  3. Symfony has some tools that helps to build APIs •

    Routing • HTTP Foundation • Serializer • Forms
  4. What does FOSRestBundle do? • Body decoding • Multiple output

    formats • Exception handling • Automatic routing • Content negotiation • ...
  5. What does JMSSerializerBundle do? • Integrates JMSSerializer lib into Symfony2

    • Enables (de)serialization of data • Highly configurable • Supports versioning and exclusion strategies
  6. What does NelmioApiDocBundle do? • Allows to generate documentation for

    APIs • Introspection to keep docs up to date • Integrates with forms • Integrates with other bundles annotations • Sandbox
  7. What does Symfony Forms do? • Makes easier to deal

    with input data • Integrates well with Symfony validator • Together with entities can act as “contract” with the “world”
  8. Some possibilities to test APIs • Unit testing ◦ Test

    lower layers with unit tests • Functional testing ◦ Test the whole API with Symfony client
  9. Guzzle “Guzzle is a PHP HTTP client and framework for

    building RESTful web service clients.”
  10. Guzzle Service Description “Guzzle allows you to serialize HTTP requests

    and parse HTTP responses using a DSL called a service descriptions. Service descriptions define web service APIs by documenting each operation … Guzzle's service descriptions are heavily inspired by Swagger.”
  11. Resource POST (create) GET (read) PUT (update) DELETE (delete) /speakers

    new speaker list speaker bulk update delete all speakers /speakers/1 error show speaker replace speaker delete speaker
  12. Additional Resources 1/2 • Blogs ◦ REST APIs with Symfony2:

    The Right Way ▪ http://williamdurand.fr/2012/08/02/rest-apis-with-symfony2- the-right-way/ ◦ Symfony2 REST API: the best way ▪ http://welcometothebundle.com/symfony2-rest-api-the- best-2013-way/ ▪ …
  13. Additional Resources 3/3 • Bundles/libs ◦ https://github.com/FriendsOfSymfony/FOSRestBundle ◦ https://github.com/nelmio/NelmioApiDocBundle ◦

    https://github.com/willdurand/BazingaHateoasBundle ◦ https://github.com/willdurand/Negotiation ◦ https://github.com/guzzle/guzzle