Slide 1

Slide 1 text

HOW JSON-API CAN BRING SANITY TO YOUR PHP APIs

Slide 2

Slide 2 text

HENNING GLATTER-GÖTZ @hglattergotz

Slide 3

Slide 3 text

Podcasting at ... REACTIVE.AUDIO Panel discussions about current tech news with a focus on JavaScript

Slide 4

Slide 4 text

... and also podcasting at DESCRIPTIVE.AUDIO Programmer origin stories

Slide 5

Slide 5 text

MY APIs ARE NOT INSANE!

Slide 6

Slide 6 text

REST IS VERY FLEXIBLE

Slide 7

Slide 7 text

WHAT IS SO BAD ABOUT THAT??

Slide 8

Slide 8 text

bikeshedding

Slide 9

Slide 9 text

SEVERAL ATTEMPTS TO SOLVE THIS EXIST HAL, Siren, OData, JSON-API

Slide 10

Slide 10 text

COVERED BY JSON-API SPEC Content Negotiation Document Structure Fetching Data Creating, Updating and Deleting Resources Query Parameters Errors

Slide 11

Slide 11 text

CONTENT NEGOTIATION Content-Type: application/vnd.api+json

Slide 12

Slide 12 text

DOCUMENT STRUCTURE

Slide 13

Slide 13 text

SINGLE RESOURCE { "data": { "type": "articles", "id": "1", "attributes": { "title": "Hello world", "body": "bla bla bla" } } }

Slide 14

Slide 14 text

SINGLE RESOURCE WITH RELATIONSHIP { "data": { "type": "articles", "id": "1", "attributes": { "title": "Hello world", "body": "bla bla bla" }, "relationships": { "author": { "links": { "self": "http://example.com/articles/1/relationships/author", "related": "http://example.com/articles/1/author" }, "data": { "type": "people", "id": "9" } } } }, "included": [] }

Slide 15

Slide 15 text

FETCHING DATA - SPARSE FIELD SETS GET /articles?fields[articles]=title,body HTTP/1.1 Accept: application/vnd.api+json

Slide 16

Slide 16 text

FETCHING DATA - SORTING ascending GET /articles?sort=created HTTP/1.1 descending GET /articles?sort=-created,title HTTP/1.1

Slide 17

Slide 17 text

ERRORS HTTP/1.1 422 Unprocessable Entity Content-Type: application/vnd.api+json { "errors": [ { "status": "422", "source": { "pointer": "/data/attributes/first-name" }, "title": "Invalid Attribute", "detail": "First name must contain at least three characters." } ] }

Slide 18

Slide 18 text

SO HOW DOES ANY OF THIS HELP US?

Slide 19

Slide 19 text

tooling

Slide 20

Slide 20 text

CLIENT LIBRARIES PHP, JavaScript, iOS, Ruby

Slide 21

Slide 21 text

SERVER LIBRARIES PHP, Node.js, Ruby, Python, Go, .NET, Java

Slide 22

Slide 22 text

PHP SERVER LIBRARIES * GOintegro/HATEOAS - Symfony 2 /Doctrine 2 * tobscure/json-api * neomerx/json-api - framework agnostic * neomerx/limoncello-collins Laravel-based quick start server * lode/jsonapi * woohoolabs/yin * nilportugues/json-api * nilportugues/symfony2-jsonapi-transformer - Symfony 2 * nilportugues/laravel5-jsonapi-transformer - Laravel 5

Slide 23

Slide 23 text

QUICK LOOK AT neomerx/json-api

Slide 24

Slide 24 text

HAND CRAFTED // Lots of hand written code here // : // to manually build array representation of document structure return json_encode($data);

Slide 25

Slide 25 text

NEOMERX/JSON-API 01 $encoder = Encoder::instance([ 02 ArticleModel::class => ArticleSchema::class 03 AuthorModel::class => AuthorSchema::class 04 ], 05 new EncoderOptions($encodeOptions, 'http://example.com') 06 ); 07 08 return $encoder->encodeData($articles);

Slide 26

Slide 26 text

Thank You! @hglattergotz