|
| 1 | +<?php |
| 2 | +// --- |
| 3 | +// slug: http-cache-tags |
| 4 | +// name: Customize HTTP Cache Tags |
| 5 | +// executable: true |
| 6 | +// position: 21 |
| 7 | +// --- |
| 8 | + |
| 9 | +// This guide describes how to customize HTTP Cache Tags included in the API response header by registering a TagCollector service. In this example, the Id |
| 10 | +// of a resource will be used to generate the cache tag instead of its IRI. |
| 11 | +// |
| 12 | +// If you only want to add additional tags without changing them, it might be sufficient to follow the [standard documentation on http cache](https://api-platform.com/docs/core/performance/#extending-cache-tags-for-invalidation). |
| 13 | +// |
| 14 | +// First, we need our book resource. For the sake of this example, we assume the id to be unique across all resources (otherwise replacing the IRI with Id would make no sense). |
| 15 | +namespace App\ApiResource { |
| 16 | + use ApiPlatform\Metadata\ApiResource; |
| 17 | + use ApiPlatform\Metadata\ApiProperty; |
| 18 | + use ApiPlatform\Metadata\Get; |
| 19 | + use ApiPlatform\Metadata\Operation; |
| 20 | + |
| 21 | + #[ApiResource( |
| 22 | + operations: [ |
| 23 | + new Get(provider: Book::class.'::provide'), |
| 24 | + ], |
| 25 | + )] |
| 26 | + class Book |
| 27 | + { |
| 28 | + #[ApiProperty(identifier: true)] |
| 29 | + public string $id = "unique-id-1"; |
| 30 | + |
| 31 | + public static function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null |
| 32 | + { |
| 33 | + return (new self()); |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// Now, we define our TagCollector service. Check the PHPDoc in TagCollectorInterface to see the various information, which is available within `$context`. |
| 39 | +// |
| 40 | +// Add any tags you want to be included in the response to `$context['resources']`. |
| 41 | + |
| 42 | +namespace App\Service { |
| 43 | + use ApiPlatform\Serializer\TagCollectorInterface; |
| 44 | + use App\ApiResource\Book; |
| 45 | + |
| 46 | + class TagCollector implements TagCollectorInterface |
| 47 | + { |
| 48 | + public function collect(array $context = []): void |
| 49 | + { |
| 50 | + if (isset($context['property_metadata'])) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + $iri = $context['iri'] ?? null; |
| 55 | + $object = $context['object'] ?? null; |
| 56 | + |
| 57 | + if ($object && $object instanceof Book) { |
| 58 | + $iri = $object->id; |
| 59 | + } |
| 60 | + |
| 61 | + if (!$iri) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + $context['resources'][$iri] = $iri; |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// Replace the service 'api_platform.http_cache.tag_collector' with your new TagCollector class. |
| 71 | + |
| 72 | +namespace App\DependencyInjection { |
| 73 | + use App\Service\TagCollector; |
| 74 | + use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; |
| 75 | + |
| 76 | + function configure(ContainerConfigurator $configurator): void |
| 77 | + { |
| 78 | + $services = $configurator->services(); |
| 79 | + $services->set('api_platform.http_cache.tag_collector', TagCollector::class); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | +// Request the book. When using Swagger UI, you should see that your 'Cache-Tags' header now includes the Id only instead of the full Iri. |
| 85 | + |
| 86 | +namespace App\Playground { |
| 87 | + use Symfony\Component\HttpFoundation\Request; |
| 88 | + |
| 89 | + function request(): Request |
| 90 | + { |
| 91 | + return Request::create('/books/unique-id-1.jsonld', 'GET'); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// ...or verify directly in testing. |
| 96 | + |
| 97 | +namespace App\Tests { |
| 98 | + use ApiPlatform\Playground\Test\TestGuideTrait; |
| 99 | + use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; |
| 100 | + |
| 101 | + final class BookTest extends ApiTestCase |
| 102 | + { |
| 103 | + use TestGuideTrait; |
| 104 | + |
| 105 | + public function testAsAnonymousICanAccessTheDocumentation(): void |
| 106 | + { |
| 107 | + $response = static::createClient()->request('GET', '/books/unique-id-1.jsonld'); |
| 108 | + |
| 109 | + $this->assertResponseIsSuccessful(); |
| 110 | + $this->assertResponseHeaderSame('Cache-Tags', 'unique-id-1'); |
| 111 | + $this->assertJsonContains([ |
| 112 | + '@id' => '/books/unique-id-1', |
| 113 | + '@type' => 'Book', |
| 114 | + 'id' => 'unique-id-1', |
| 115 | + ]); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | +// If you rely on invalidation from Api-Platform, don't forget that you'd also need to have your own implementation of `PurgeHttpCacheListener`. Otherwise the wrong tags will be purged. |
0 commit comments