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

Take Off Conf 2016: API Platform - The PHP framework to build modern web APIs

Take Off Conf 2016: API Platform - The PHP framework to build modern web APIs

API Platform 2 is a brand new open source PHP7 framework dedicated to the creation of modern web APIs. In this talk, we'll discover how to use it to build a fully featured API in just a few lines of code. Because API Platform is built on top of the industry-leading Symfony framework, we'll see how easy it is to use advanced features.

E66449b8260b07a1cf51c5ab5eaa8180?s=128

Kévin Dunglas

October 21, 2016
Tweet

More Decks by Kévin Dunglas

Other Decks in Programming

Transcript

  1. API PLATFORM The PHP framework to build modern web APIs.

  2. Kévin Dunglas Founder of Les-Tilleuls.coop Symfony Core Team member API

    Platform creator Teacher at the University of Lille 1 @dunglas
  3. Les-Tilleuls.coop Self-managed company since 2011 100% owned by employees All

    benefits are equitably shared between employees 18 people, 137% growth in 2015 We are hiring! => jobs@les-tilleuls.coop
  4. The Web Has Changed Users spend more time on mobiles

    than on desktops and even than watching TV Data must be available everywhere, instantly and programmatically Single Page Applications are the standard Everything is connected: smart watches, cars, Amazon Echo, bots, even fridges
  5. APIs: the Heart of the New Web Central point to

    access data Encapsulate the business logic Same features for all channels: webapps, native mobile apps, IoT, external systems (customers and providers) APIs are the foundations of this new web
  6. None
  7. API: State of the Art

  8. HTTP + REST + JSON Works easily with all programming

    languages, on every platforms Lightweight (= fast) Stateless (= scale) Easy to cache (= faster) High quality tooling (cURL, Varnish…)
  9. REST: a Pattern, Not a Format 1 project ~= 1

    different implementation Tedious to create: pagination, filtering, validation, caching, content negotiation, CORS, data interoperability… Hard to reuse server-side tooling (1 API per project) Hard to reuse client-side tooling (1 client per API) Hard to query and aggregate data from different sources
  10. Credit: Martin Fowler http://martinfowler.com/articles/richardsonMaturityModel.html

  11. HATEOAS / Linked Data Hypermedia: IRIs (e.g. URLs) as identifiers

    Ability to reference external data (like hypertext links) Hypermedia controls (pagination, filtering…) The road to better server-side tooling and generic clients Hypermedia as the Engine of Application State
  12. None
  13. None
  14. Standards Please!

  15. Swagger / OpenAPI Describe only I/O formats and available operations

    for an API Most popular way to describe APIs Lot of tools available (UIs, code generators, validators…) Open standard (but not endorsed by the W3C) Not designed for HATEOAS APIs
  16. JSON-LD Standard: W3C recommandation (since 2014) Easy to use: looks

    like a typical JSON document Already chosen by Google, BBC, Microsoft, US gov... Compliant with technologies of the semantic web: RDF, SPARQL, triple store... JSON for Linked Data
  17. Schema.org Large set of elements: people, creative works, events, products,

    chemicals... Created (and understood by) Google, Bing, Yahoo! et Yandex Massively used, and hosted by the W3C (Web schemas group) Supports HTML’s microdata, RDFa and JSON-LD Open vocabulary for data interoperability at web scale Extension mechanism, compatible with proprietary/custom vocabularies (yours)
  18. Hydra Write support (POST, PUT, PATCH…) Make APIs auto-discoverable (all

    available operations are documented) A standard for describing collections, paginations, filters, errors… Draft W3C (Work In Progress) Describe REST APIs in JSON-LD
  19. Getting Started with API Platform A framework for the new

    web.
  20. The Promise Support for modern formats (JSON-LD, Hydra, HAL, schema.org,

    API+Problem…) Batteries included: pagination, filtering, auth (JWT, OAuth), HTTP Cache, CORS… Awesome UI and automatic documentation (Swagger) Extensible, overridable, customizable A working API in a few minutes
  21. Install Get Docker If you don’t already have it…

  22. Install # Download the latest version from api-platform.com or GitHub

    $ wget https://api.github.com/repos/api-platform/api-platform/zipball # Start the LAMP stack (PHP7, Apache and MySQL) $ docker-compose up # Install dependencies $ docker-compose run web composer install --no-interaction # Create the database $ docker-compose run web bin/console doctrine:schema:create
  23. It’s Symfony… Configured with the most popular libraries for APIs

    Compatible with all existing bundles Use Doctrine ORM by default (but you can use the persistence system you want) Symfony full stack application …with something more
  24. None
  25. Create your Own Data Model Write some PHPDoc (optional) Add

    the @ApiResource annotation Map its properties using the Doctrine ORM Update the database schema $ docker-compose run web bin/console doctrine:schema:update --force Create a Plain Old PHP Object
  26. Your 1st API Platform App is Up and Running!

  27. None
  28. Add a Book

  29. Add a Book

  30. Out of the Box Features JSON-LD + Hydra formats Swagger

    documentation Fully featured UI (working for all URLs of the API) Create (POST), Retrieve (GET item and lists), Update (PUT) and Delete (DELETE) resources Pagination for lists (30 items per page), fully configurable
  31. Add some validation rules Many validation constraints available Ability to

    create custom constraints Use the Symfony Validator component
  32. Data Validation

  33. Data Validation

  34. Content Negotiation Adding a new format is as simple as

    creating a new Symfony Normalizer for it Built-in formats: JSON-LD, HAL, XML, YAML, CSV, JSON, HTML (UI) To retrieve a resource in a given format: add an Accept HTTP header or use the format name as file extension
  35. Content Negotiation

  36. Content Negotiation: HAL

  37. Content Negotiation: XML

  38. Filters Register the filter service (built-in or custom): Map the

    filter to the resource
  39. Filters Register the filter service (built-in or custom): Map the

    filter to the resource
  40. Filters

  41. The Event System

  42. Events

  43. The Schema Generator Pick an existing data model from (resources

    and properties) from schema.org:
  44. The Schema Generator # Download the generator wget https://github.com/api-platform/schema-generator/releases/download/v1.1.2/ schema.phar

    # Execute it docker-compose web run schema.phar generate-types src/ app/config/ schema.yml
  45. The Schema Generator PHP classes, properties, getters and setters (PSR

    compliant) Doctrine ORM mapping (including relations and mapped superclasses) Validation constraints Full PHPDoc extracted from schema human-readable descriptions Mapping with schema.org's IRIs The generator uses schema.org data to automatically bootstrap: Relations between classes (supported by the API system too)
  46. The Schema Generator

  47. There is Much More to See

  48. A Growing Community ~100 awesome contributors (code and docs) 800+

    stars on GitHub 480 followers on Twitter 20 people, 100% growth in 2015
  49. They Already Use API Platform

  50. Next Big Things (v2.1) JSONAPI support GraphQL support Native MongoDB

    support Your contribution?
  51. Thanks! Any questions? api-platform/api-platform @ApiPlatform https://api-platform.com