Skip to content

Commit bcfd40e

Browse files
docs(identifiers): compatibility with laravel (#2091)
1 parent d592901 commit bcfd40e

File tree

1 file changed

+42
-13
lines changed

1 file changed

+42
-13
lines changed

core/identifiers.md

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ Let's say you have the following class, which is identified by a `UUID` type. In
1313

1414
```php
1515
<?php
16-
// api/src/Entity/Person.php
17-
namespace App\Entity;
16+
// api/src/ApiResource/Person.php with Symfony or app/ApiResource/Person.php with Laravel
17+
namespace App\ApiResource;
1818

1919
use ApiPlatform\Metadata\ApiProperty;
2020
use ApiPlatform\Metadata\ApiResource;
@@ -36,38 +36,40 @@ final class Person
3636

3737
```yaml
3838
# api/config/api_platform/resources/Person.yaml
39+
# The YAML syntax is only supported for Symfony
3940
properties:
40-
App\Entity\Person:
41+
App\ApiResource\Person:
4142
code:
4243
identifier: true
4344
resource:
44-
App\Entity\Person:
45+
App\ApiResource\Person:
4546
provider: App\State\PersonProvider
4647
```
4748
4849
```xml
50+
<!-- The XML syntax is only supported for Symfony -->
4951
<properties xmlns="https://api-platform.com/schema/metadata/properties-3.0">
50-
<property resource="App\Entity\Person" name="code" identifier="true"/>
52+
<property resource="App\ApiResource\Person" name="code" identifier="true"/>
5153
</properties>
5254
<resources xmlns="https://api-platform.com/schema/metadata/resources-3.0">
53-
<resource class="App\Entity\Person" provider="App\State\PersonProvider" />
55+
<resource class="App\ApiResource\Person" provider="App\State\PersonProvider" />
5456
</resources>
5557
```
5658

5759
</code-selector>
5860

59-
Once registered as an `ApiResource`, having an existing person, it will be accessible through the following URL: `/people/110e8400-e29b-11d4-a716-446655440000`.
60-
Note that the property identifying our resource is named `code`.
61+
Once registered as an `ApiResource`, having an existing person, it will be accessible through the following URL:
62+
`/people/110e8400-e29b-11d4-a716-446655440000`. Note that the property identifying our resource is named `code`.
6163

62-
Let's create a `Provider` for the `Person` entity:
64+
Let's create a `Provider` for the `Person` resource:
6365

6466
```php
6567
<?php
66-
// api/src/State/PersonProvider.php
68+
// api/src/State/PersonProvider.php with Symfony or app/State/PersonProvider.php with Laravel
6769

6870
namespace App\State;
6971

70-
use App\Entity\Person;
72+
use App\ApiResource\Person;
7173
use ApiPlatform\Metadata\Operation;
7274
use ApiPlatform\State\ProviderInterface;
7375
use App\Uuid;
@@ -92,7 +94,7 @@ This case is covered by an URI variable transformer:
9294

9395
```php
9496
<?php
95-
// api/src/Identifier/UuidUriVariableTransformer.php
97+
// api/src/Identifier/UuidUriVariableTransformer.php with Symfony or app/Identifier/UuidUriVariableTransformer.php with Laravel
9698
namespace App\Identifier;
9799

98100
use ApiPlatform\Api\UriVariableTransformerInterface;
@@ -139,12 +141,15 @@ final class UuidUriVariableTransformer implements UriVariableTransformerInterfac
139141
}
140142
```
141143

142-
Tag this service as an `api_platform.uri_variables.transformer`:
144+
Tag this service as an `api_platform.uri_variables.transformer` using one of the configurations below:
145+
146+
### Tag the Service using Symfony
143147

144148
<code-selector>
145149

146150
```yaml
147151
# api/config/services.yaml
152+
# The YAML syntax is only supported for Symfony
148153

149154
services:
150155
App\Identifier\UuidUriVariableTransformer:
@@ -153,6 +158,8 @@ services:
153158
```
154159
155160
```xml
161+
<!-- The XML syntax is only supported for Symfony -->
162+
156163
<service id="App\Identifier\UuidUriVariableTransformer" class="App\Identifier\UuidUriVariableTransformer" public="false">
157164
<tag name="api_platform.uri_variables.transformer" />
158165
</service>
@@ -162,6 +169,28 @@ services:
162169

163170
Your `PersonProvider` will now work as expected!
164171

172+
### Tag the Service using Laravel
173+
174+
```php
175+
<?php
176+
177+
namespace App\Providers;
178+
179+
use App\Identifier\UuidUriVariableTransformer;
180+
use ApiPlatform\Metadata\UriVariableTransformerInterface;
181+
use Illuminate\Support\ServiceProvider;
182+
183+
class AppServiceProvider extends ServiceProvider
184+
{
185+
public function register(): void
186+
{
187+
$this->app->tag([UuidUriVariableTransformer::class], UriVariableTransformerInterface::class);
188+
}
189+
}
190+
```
191+
192+
Your `PersonProvider` will now work as expected!
193+
165194
## Changing Identifier in a Doctrine Entity
166195

167196
If your resource is also a Doctrine entity and you want to use another identifier other than the Doctrine one, you have to unmark it:

0 commit comments

Comments
 (0)