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.

Kévin Dunglas

October 21, 2016
Tweet

More Decks by Kévin Dunglas

Other Decks in Programming

Transcript

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

    Platform creator Teacher at the University of Lille 1 @dunglas
  2. 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! => [email protected]
  3. 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
  4. 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
  5. 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…)
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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)
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. Add some validation rules Many validation constraints available Ability to

    create custom constraints Use the Symfony Validator component
  18. 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
  19. 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
  20. 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)
  21. A Growing Community ~100 awesome contributors (code and docs) 800+

    stars on GitHub 480 followers on Twitter 20 people, 100% growth in 2015