Lead-Developper & Formateur @ Les-Tilleuls.coop
@gheb_dev
Dev/consulting/training - Lille/Paris/Amiens
👊 Auto-gérée depuis 2011
⬆ 97% en 2016, 23 employés modèles
masterclass@les-tilleuls.coop
Ou tout autre client (mobile, etc...)
s les
Hypermedia as the Engine of Application State
Json for Linked data
{
"@context": "/contexts/PostalAddress",
"@id": "/postal_addresses",
"@type": "hydra:PagedCollection",
"hydra:totalItems": 1,
"hydra:itemsPerPage": 30,
"hydra:firstPage": "/postal_addresses",
"hydra:lastPage": "/postal_addresses",
"hydra:member": [
{
"@id": "/postal_addresses/1",
"@type": "http://schema.org/PostalAddress",
"id": 1,
"addressCountry": "France",
"postalCode": "75008",
"streetAddress": "6 Rue Balzac"
}
]
}
api_platform:
# ...
formats:
# ...
jsonapi: ['application/vnd.api+json']
error_formats:
# ...
jsonapi: ['application/vnd.api+json']
{
"links": {
"self": "http://example.com/articles",
"next": "http://example.com/articles?page[offset]=2",
"last": "http://example.com/articles?page[offset]=10"
},
"data": [{
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
},
"relationships": {
"author": {
"links": {
"self": "http://example.com/articles/1/relationships/author",
"related": "http://example.com/articles/1/author"
},
"data": { "type": "people", "id": "9" }
},
"comments": {
"links": {
"self": "http://example.com/articles/1/relationships/comments",
"related": "http://example.com/articles/1/comments"
},
"data": [
{ "type": "comments", "id": "5" },
{ "type": "comments", "id": "12" }
]
}
},
"links": {
"self": "http://example.com/articles/1"
}
}],
"included": [{
"type": "people",
"id": "9",
"attributes": {
"first-name": "Dan",
"last-name": "Gebhardt",
"twitter": "dgeb"
},
"links": {
"self": "http://example.com/people/9"
}
}, {
"type": "comments",
"id": "5",
"attributes": {
"body": "First!"
},
"relationships": {
"author": {
"data": { "type": "people", "id": "2" }
}
},
"links": {
"self": "http://example.com/comments/5"
}
}, {
"type": "comments",
"id": "12",
"attributes": {
"body": "I like XML better"
},
"relationships": {
"author": {
"data": { "type": "people", "id": "9" }
}
},
"links": {
"self": "http://example.com/comments/12"
}
}]
}
{
"@context": "http://schema.org",
"@id": "/postal_addresses/1",
"@type": "http://schema.org/PostalAddress",
"id": 1,
"addressCountry": "France",
"postalCode": "75008",
"streetAddress": "6 Rue Balzac"
}
# app/config/config.yml
api_platform:
collection:
pagination:
partial: false # Default value
client_partial: false # Default value
partial_parameter_name: 'partial' # Default value
# api_platform.yaml
services:
offer.search_filter:
parent: 'api_platform.doctrine.orm.search_filter'
arguments: [ { id: 'exact', price: 'exact', name: 'partial' } ]
tags: [ 'api_platform.filter' ]
<?php
// src/AppBundle/Entity/Offer.php
namespace AppBundle\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
/**
* @ApiResource(attributes={"filters"={"offer.search_filter"}})
*/
class Offer
{
// ...
}
<?php
// src/AppBundle/Entity/Offer.php
namespace AppBundle\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiFilter;
/**
* @ApiResource
* @ApiFilter(SearchFilter::class, strategy="partial")
*/
class Offer
{
// ...
}
$ wget https://github.com/api-platform/api-platform/archive/v2.2.0-beta.1.tar.gz
$ tar xzvf v2.2.0-beta.1.tar.gz
$ cd api-platform-2.2.0
$ composer create-project symfony/skeleton MyProject
$ composer req api
# Start Docker
$ docker-compose up -d
# Création de la bdd
$ docker-compose exec php bin/console doctrine:schema:create
API de données auto-decouvrable !
Swagger UI Détecte et documente automatiquement les ressources exposées.
L'admin s'appuie sur la documentation Hydra pour générer les opérations CRUD nécessaire !
Le client s'appuie sur la documentation Hydra pour générer les vues et composants REACT !
$ helm install ./api/helm/api --name api
Le projet est déployé, managé
et scalé (j'avais plus le mot français)