@gheb_dev
Ou tout autre client (mobile,etc...)
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"
}
]
}
$ wget https://github.com/api-platform/api-platform/archive/v2.2.0.tar.gz
$ tar xzvf v2.2.0.tar.gz
$ cd api-platform-2.2.0
$ composer create-project api-platform/api-platform formation
# Start Docker
$ docker-compose up -d
# Création de la bdd
$ docker-compose exec php bin/console doctrine:schema:create
<?php
declare(strict_types=1);
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
/**
* The mailing address.
*
* @see http://schema.org/PostalAddress Documentation on Schema.org
*
* @ORM\Entity
* @ApiResource(iri="http://schema.org/PostalAddress")
*/
class PostalAddress
{
/**
* @ORM\Id
* @ORM\Column(type="string")
* @ORM\GeneratedValue(strategy="UUID")
*
* @var string
*/
private $uuid;
/**
* @var string|null The country. For example, USA. You can also provide the two-letter \[ISO 3166-1 alpha-2 country code\](http://en.wikipedia.org/wiki/ISO\_3166-1).
*
* @ORM\Column(type="text", nullable=true)
* @ApiProperty(iri="http://schema.org/addressCountry")
*/
private $addressCountry;
/**
* @var string|null The postal code. For example, 94043.
*
* @ORM\Column(type="text", nullable=true)
* @ApiProperty(iri="http://schema.org/postalCode")
*/
private $postalCode;
/**
* @var string|null The street address. For example, 1600 Amphitheatre Pkwy.
*
* @ORM\Column(type="text", nullable=true)
* @ApiProperty(iri="http://schema.org/streetAddress")
*/
private $streetAddress;
public function getId(): ?int
{
return $this->id;
}
public function setAddressCountry(?string $addressCountry): void
{
$this->addressCountry = $addressCountry;
}
public function getAddressCountry(): ?string
{
return $this->addressCountry;
}
public function setPostalCode(?string $postalCode): void
{
$this->postalCode = $postalCode;
}
public function getPostalCode(): ?string
{
return $this->postalCode;
}
public function setStreetAddress(?string $streetAddress): void
{
$this->streetAddress = $streetAddress;
}
public function getStreetAddress(): ?string
{
return $this->streetAddress;
}
}
$ docker-compose exec php bin/console \
doctrine:schema:update --force
api-# \d+ postal_address
id | integer | not null | plain | |
address_country | text | | extended | |
postal_code | text | | extended | |
street_address | text | | extended | |
http(s)://localhost:8080/
{
"@context": "/contexts/PostalAddress",
"@id": "/postal_addresses",
"@type": "hydra:Collection",
"hydra:member": [
{
"@id": "/postal_addresses/1",
"@type": "http://schema.org/PostalAddress",
"id": 1,
"addressCountry": "France",
"postalCode": "75008",
"streetAddress": "6 Rue Balzac"
}
],
"hydra:totalItems": 1
}
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 !
<?php
declare(strict_types=1);
namespace App\Entity;
use ...
use Symfony\Component\Validator\Constraints as Assert;
/**
* The mailing address.
*
* @see http://schema.org/PostalAddress Documentation on Schema.org
*
* @ORM\Entity
* @ApiResource(iri="http://schema.org/PostalAddress")
*/
class PostalAddress
{
// ...
/**
* @var string|null The street address. For example, 1600 Amphitheatre Pkwy.
*
* @Assert\NotBlank
*
* @ORM\Column(type="text", nullable=true)
* @ApiProperty(iri="http://schema.org/streetAddress")
*/
private $streetAddress;
// ...
}