The international conference on the API Platform Framework
The API Platform Conference 2024 is happening soon, and it's close to selling out.
API Platform 4, Caddy web server, Xdebug, AI... Enjoy two days of inspiring talks with our friendly community and our amazing speakers.
// src/App/ApiResource.php
namespace App\ApiResource;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Operation;
#[ApiResource(
operations: [
new Get(provider: Book::class.'::provide'),
],
)]
class Book
{
#[ApiProperty(identifier: true)]
public string $id = "unique-id-1";
public static function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
return (new self());
}
}
// src/App/Service.php
namespace App\Service;
use ApiPlatform\Serializer\TagCollectorInterface;
use App\ApiResource\Book;
class TagCollector implements TagCollectorInterface
{
public function collect(array $context = []): void
{
if (isset($context['property_metadata'])) {
return;
}
$iri = $context['iri'] ?? null;
$object = $context['object'] ?? null;
if ($object && $object instanceof Book) {
$iri = $object->id;
}
if (!$iri) {
return;
}
$context['resources'][$iri] = $iri;
}
}
// src/App/DependencyInjection.php
namespace App\DependencyInjection;
use App\Service\TagCollector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
function configure(ContainerConfigurator $configurator): void
{
$services = $configurator->services();
$services->set('api_platform.http_cache.tag_collector', TagCollector::class);
}
// src/App/Playground.php
namespace App\Playground;
use Symfony\Component\HttpFoundation\Request;
function request(): Request
{
return Request::create('/books/unique-id-1.jsonld', 'GET');
}
// src/App/Tests.php
namespace App\Tests;
use ApiPlatform\Playground\Test\TestGuideTrait;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
final class BookTest extends ApiTestCase
{
use TestGuideTrait;
public function testAsAnonymousICanAccessTheDocumentation(): void
{
$response = static::createClient()->request('GET', '/books/unique-id-1.jsonld');
$this->assertResponseIsSuccessful();
$this->assertResponseHeaderSame('Cache-Tags', 'unique-id-1');
$this->assertJsonContains([
'@id' => '/books/unique-id-1',
'@type' => 'Book',
'id' => 'unique-id-1',
]);
}
}
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