api platform 2.1: when symfony meets reactjs (symfony live 2017)

99
API PLATFORM 2.1 When Symfony meets React

Upload: les-tilleulscoop

Post on 21-Apr-2017

3.690 views

Category:

Internet


2 download

TRANSCRIPT

Page 1: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

API PLATFORM 2.1When Symfony meets React

Page 2: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Kévin DunglasFounder of Les-Tilleuls.coop

Symfony Core Team

API Platform creator

@dunglas

Page 3: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Les-Tilleuls.coopSelf-managed company since 2011

100% owned by employees

19 people, 97% growth in 2016

Hiring in Lille, London and Amiens: [email protected]

Page 4: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

The Web of 2017

Page 5: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)
Page 6: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

APIs: the Heart of the New Web

Central point to access data

Encapsulate the business logic

Same features available for all channels:webapps, native mobile apps, IoT, enterprise, customers and providers information systems

APIs are the foundations of this new web

Page 7: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

SPA: Rich and Fast Webapps

Downloaded and executed only 1 time (first page load)

Use asynchronous HTTP requests to retrieve and modify the raw data (usually JSON) using an API

Huge ecosystem: React/Redux, Angular, Vue, Ember… and probably a new one while I’m speaking

Holds the presentation logic (good-bye Twig)

Standalone apps built using JavaScript, HTML and CSS

Modernized stack: ES2015/16/17, Babel, Webpack, Yarn, Flow, TypeScript…

Page 8: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Native Mobile Apps

Use HTTP requests to retrieve and modify the raw data (usually JSON) using an API but can also work offline

Can be developed using the JS stack: React Native, NativeScript (Angular) - good-bye Cordova

Downloaded from stores (App Store, Google Play)

Look’n’Feel consistent with the platform (iOS or Android)

Page 9: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

API Platform:a Framework for 2017

Page 10: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Goals of the Framework

Support modern open API formats

Create a rock-solid API-first system in minutes

Batteries included: everything to create and consume the API

Be able to override, extend and customize everything

Page 11: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Features of API Platform 2.0

Page 12: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

They Already Use API Platform

Page 13: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Getting Started

Page 14: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Install

Get DockerIf you don’t already have it…

Page 15: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Install

Provided Docker images - PHP, Nginx, MySQL and Varnish (optional) optimized for Symfony:

Create the database:

Download and extract the ZIP from api-platform.com

Browse http://localhost

$ docker-compose up

$ docker-compose exec php bin/console doctrine:schema:create

Page 16: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)
Page 17: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

A working APIwith just 1 PHP class

Page 18: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Your Turn!

Page 19: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Create your Own Data Model

Write some PHPDoc (optional)

Add the @ApiResource annotation

Map its properties using theDoctrine ORM (optional)

Update the database schema

$ docker-compose run web bin/console doctrine:schema:update --force

Create a Plain Old PHP Object

Page 20: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Your API is Ready!

Page 21: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Out of the Box Features

JSON-LD + Hydra encoding (HAL, XML, YAML, CSV and raw JSON can be enabled through config and you can add yours)

Swagger documentation

Fully featured JavaScript UI using Swagger UI (now built with React) available for all URLs of the API

Create (POST), Retrieve (GET item and lists), Update (PUT) and Delete (DELETE) operations

Pagination for lists (30 items per page), fully configurable

Page 22: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)
Page 23: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

What’s this Format?JSON-LD and Hydra

Page 24: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Credit: Martin Fowlerhttp://martinfowler.com/articles/richardsonMaturityModel.html

Page 25: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

JSON-LDStandardized hypermedia format: W3C recommandation (2014)

Easy to use: a standard JSON document with some specials keys (starting with @) and mapped with a context

Backed by Google, BBC, Microsoft, US & UK govs...

Compliant with technologies of the semantic web: RDF, SPARQL, triple store...

JSON for Linked Data

Page 26: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)
Page 27: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)
Page 28: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)
Page 29: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)
Page 30: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)
Page 31: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)
Page 32: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Hydra

Make the API auto-discoverable by clients: all available resources, properties, types and operations are described

Provide a standard format for common structures: collections, paginations, filters, errors…

Documentation of the REST API in JSON-LD

Draft W3C

Page 33: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)
Page 34: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)
Page 35: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)
Page 36: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Data Validation

Page 37: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Addvalidation rules

Many validation constraints available

Ability to create custom constraints

Use the Symfony Validatorcomponent

Page 38: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)
Page 39: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Relations

Page 40: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Add a Relation

Page 41: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)
Page 42: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Embedding Relations

Page 43: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Embedded Relations

Page 44: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

How does it Work?

Page 45: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

REST

Wikipedia

Page 46: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Lexicon

Operation: action on an item or on a collection, usually through HTTP. Internally a Symfony route.

DELETE /books/22

Resource: data exposed by the API, identified by an URL. Internally a PHP object (an entity), externally a document (JSON, HTML, XML…).

item: /books/22 collection: /books

Property: property of a resource. Internally, a class’ property. Externally a document field (e.g. a JSON property). Typed.

Page 47: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

It’s Symfony…

Configured with the most popular librariesfor APIsCompatible with all existing bundles

Use Doctrine ORM by default (but you canuse the persistence system you want)

Symfony full stack application

Page 48: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

…with something more

Properties’ types and relations are guessed using the Symfony PropertyInfo component

The Metadata component guesses the API’s data structure (resources and properties) form PHP classes marked with @ApiResource

The structure is stored in an intermediate representation used to serialize data and generate API docs (Hydra, Swagger…)

Page 49: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

And Even More…Symfony routes are automatically registered for all operations on collections and items. By default:

item: GET, PUT, DELETEcollection: GET, POST

Data is normalized and denormalized using hypermedia normalizers (JSON-LD, Hydra, HAL…) for the Symfony Serializer Component

Page 50: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Event-Driven Architecture

Page 51: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Client-Side Tooling

Page 52: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)
Page 53: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Foundation of the most popular JS ecosystem out there

A JavaScript library for building user interfacesCreated by Facebook

JSX: write markup inside code

Page 54: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)
Page 55: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Just like Composer, but for JavaScript.

$ curl -o- -L https://yarnpkg.com/install.sh | bash

$ brew install yarn Mac users:

Page 56: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Create React App

Babel: a JavaScript compiler (ES2015+, JSX, Flow…)

An app skeleton generator

All tools and configs to get started:

Webpack: a module bundler (compile and package your JS, CSS…)

$ yarn global add create-react-app $ create-react-app my-app $ cd my-app $ yarn start

ESLint: identify and report bugs and bad patterns in the code

Integrated web server, live reloading, development and debugging tools

Page 57: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)
Page 58: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

A Basic React Component

Page 59: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)
Page 60: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Adding some Properties

Page 61: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Adding some Properties

Page 62: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Configuring CORS headers

Page 63: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Loading Some Fixtures

Page 64: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Loading some Fixtures

$ docker-compose exec php bin/console hautelook:fixtures:load

Page 65: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

The Admin Generator

Page 66: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

API Platform AdminLike Sonata or EasyAdminBundle but as a React SPA

Built on top of Admin On Rest (by Marmelab)

$ yarn add api-platform-admin

Material Design UI

Consume the API using AJAX

Automatically generate the admin by parsing the Hydra API doc

Page 67: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

API Platform Admin

Page 68: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)
Page 69: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Customizing the Admin

Page 70: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)
Page 71: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

The CRUD Generator

Page 72: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

API Platform Generate CRUDLike Sensio Generator Bundle, but 100% client-side

Rock solid stack

$ yarn global add api-platform-generate-crud

Twitter Bootstrap support + accessibility (ARIA roles)

Consume the API using ES2015’s fetch()

Automatically generate files by parsing the Hydra API doc

Page 73: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

The StackReact, ES2015+, JSX

React Router: client-side routing library

Redux: an extensible container to manage the states of the app

Redux Form: manage form states

Redux Thunk: asynchronous actions (AJAX requests)

Optional: Twitter Bootstrap

Page 74: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Redux

css-tricks.com

Page 75: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)
Page 76: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)
Page 77: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Register the Generated code

Page 78: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)
Page 79: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

The API Client

Page 80: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Actions and Creators

Page 81: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Reducers

Page 82: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Components…

Page 83: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

…Components Wiring

Page 84: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

The Form

Page 85: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

React Native Generator

Coming Soon!

Page 86: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Thanks!Any questions?

api-platform/api-platform @ApiPlatform

https://api-platform.com

Page 87: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Extra: More Features!

Page 88: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

New in API Platform 2.1

An API as fast as a static website: builtin invalidation-based HTTP cache (Varnish and CloudFlare support)

Client-side tools to consume the API built with React and Redux: an app generator and an admin

Advanced, per-operation authorization rules

Native sub-resources support (e.g. GET /users/1/comments)

A ton of Developer eXperience improvements and better docs

Page 89: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

FiltersRegister the filter service (built-in or custom):

Map the filter to the resource

Page 90: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Filters

Page 91: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

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

Page 92: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Content Negotiation

Page 93: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Content Negotiation: HAL

Page 94: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Content Negotiation: XML

Page 95: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

Events

Page 96: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

The Schema GeneratorPick an existing data model from (resources and properties) from schema.org:

$ docker-compose exec php vendor/bin/schema generate-types src/ schema.yml

Page 97: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

The Schema Generator

Page 98: API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)

The Schema Generator

PHP classes, properties, getters and setters (PSR compliant)Doctrine ORM mapping (including relations and mapped superclasses)Validation constraintsFull PHPDoc extracted from schema human-readable descriptionsMapping with schema.org's IRIs

The generator uses schema.org data to automatically bootstrap:

Relations between classes (supported by the API system too)