This document introduces API Platform, an open-source PHP framework for building web APIs. It provides out-of-the-box features like JSON-LD support, Swagger documentation, and a fully-featured UI. API Platform is built on Symfony and uses Doctrine ORM by default. It allows developers to quickly create RESTful APIs from PHP entities with automated CRUD functionality. The framework also supports content negotiation, filtering, embedded relations, and has a growing community of contributors.
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 Promise
Support for modern and future formats
Batteries included: pagination, filtering, auth (JWT, OAuth),
HTTP Cache, CORS…
UI and automatic documentation (Swagger)
Extensible, overridable, customizable
A working API in a few minutes
6. 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…)
7. 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
9. 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. 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
13. 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
14. 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)
15. 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. The Promise
Support for modern and future 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
Start the LAMP stack (PHP7, Apache and MySQL)
$ docker-compose up
# Create the database
$ docker-compose exec web bin/console doctrine:schema:create
Go to api-platform.com then click « Download »
Done! Browse http://localhost.
22.
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. 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
27. 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
30. The Schema Generator
Pick an existing data model from (resources and properties) from
schema.org:
docker-compose run web
vendor/bin/schema generate-types src/ app/config/schema.yml
32. 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)
37. 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