The international conference on the API Platform Framework
Be part of the very first meeting with the FrankenPHP elePHPant plushies in Lille.
This edition is shaping up to be our biggest yet — secure your seat now before we sell out.
// src/App/ApiResource.php
namespace App\ApiResource;
use ApiPlatform\Metadata\ApiResource;
use App\State\BookProcessor;
use App\State\BookProvider;
#[ApiResource(processor: BookProcessor::class, provider: BookProvider::class)]
class Book
{
public function __construct(public string $id, public string $title)
{
}
}
// src/App/State.php
namespace App\State;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use ApiPlatform\State\ProviderInterface;
use App\ApiResource\Book;
use Ramsey\Collection\CollectionInterface;
final class BookProcessor implements ProcessorInterface
{
/**
* @param Book $data
*/
public function process($data, Operation $operation, array $uriVariables = [], array $context = []): Book
{
$id = $uriVariables['id'] ?? $data->id;
file_put_contents(sprintf('book-%s.json', $id), json_encode($data));
return $data;
}
}
final class BookProvider implements ProviderInterface
{
public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?Book
{
if ($operation instanceof CollectionInterface) {
throw new \RuntimeException('Not supported.');
}
$file = sprintf('book-%s.json', $uriVariables['id']);
if (!file_exists($file)) {
return null;
}
$data = json_decode(file_get_contents($file));
return new Book($data->id, $data->title);
}
}
// src/App/Playground.php
namespace App\Playground;
use Symfony\Component\HttpFoundation\Request;
function request(): Request
{
return Request::create(uri: '/books', method: 'POST', server: ['CONTENT_TYPE' => 'application/ld+json'], content: json_encode(['id' => '1', 'title' => 'API Platform rocks.']));
}
Made with love by
Les-Tilleuls.coop can help you design and develop your APIs and web projects, and train your teams in API Platform, Symfony, Next.js, Kubernetes and a wide range of other technologies.
Learn more