Skip to content

Commit a233c75

Browse files
authored
feat: add URL generation strategy documentation (#1377)
1 parent e633059 commit a233c75

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

core/configuration.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,7 @@ api_platform:
407407
stateless: ~
408408

409409
# The URL generation strategy to use for IRIs
410-
# Use values from UrlGeneratorInterface
411-
url_generation_strategy: ~
410+
url_generation_strategy: !php/const ApiPlatform\Core\Api\UrlGeneratorInterface::ABS_PATH
412411

413412
# ...
414413
```

core/url-generation-strategy.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# URL Generation Strategy
2+
3+
By default, API Platform generates all URLs as absolute paths to the base URL.
4+
5+
For instance, in JSON-LD, you will get a collection like this:
6+
7+
```json
8+
{
9+
"@context": "/contexts/Book",
10+
"@id": "/books",
11+
"@type": "hydra:Collection",
12+
"hydra:member": [
13+
{
14+
"@id": "/books/1",
15+
"@type": "http://schema.org/Book",
16+
"name": "My awesome book"
17+
}
18+
],
19+
"hydra:totalItems": 1
20+
}
21+
```
22+
23+
You may want to use absolute URLs (for instance if resources are used in another API) or network paths instead.
24+
25+
It can be configured globally:
26+
27+
```yaml
28+
# api/config/packages/api_platform.yaml
29+
api_platform:
30+
defaults:
31+
url_generation_strategy: !php/const ApiPlatform\Core\Api\UrlGeneratorInterface::ABS_URL
32+
```
33+
34+
It can also be configured only for a specific resource:
35+
36+
[codeSelector]
37+
38+
```php
39+
<?php
40+
// api/src/Entity/Book.php
41+
42+
use ApiPlatform\Core\Annotation\ApiResource;
43+
use ApiPlatform\Core\Api\UrlGeneratorInterface;
44+
45+
#[ApiResource(urlGenerationStrategy: UrlGeneratorInterface::ABS_URL)]
46+
class Book
47+
{
48+
// ...
49+
}
50+
```
51+
52+
```yaml
53+
# api/config/api_platform/resources.yaml
54+
App\Entity\Book:
55+
attributes:
56+
url_generation_strategy: !php/const ApiPlatform\Core\Api\UrlGeneratorInterface::ABS_URL
57+
```
58+
59+
```xml
60+
<?xml version="1.0" encoding="UTF-8" ?>
61+
<!-- api/config/api_platform/resources.xml -->
62+
63+
<resources
64+
xmlns="https://api-platform.com/schema/metadata"
65+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
66+
xsi:schemaLocation="https://api-platform.com/schema/metadata
67+
https://api-platform.com/schema/metadata/metadata-2.0.xsd">
68+
<resource class="App\Entity\Book">
69+
<attribute name="url_generation_strategy" type="constant">ApiPlatform\Core\Api\UrlGeneratorInterface::ABS_URL</attribute>
70+
</resource>
71+
</resources>
72+
```
73+
74+
[/codeSelector]
75+
76+
For the above configuration, the collection will be like this:
77+
78+
```json
79+
{
80+
"@context": "http://example.com/contexts/Book",
81+
"@id": "http://example.com/books",
82+
"@type": "hydra:Collection",
83+
"hydra:member": [
84+
{
85+
"@id": "http://example.com/books/1",
86+
"@type": "http://schema.org/Book",
87+
"name": "My awesome book"
88+
}
89+
],
90+
"hydra:totalItems": 1
91+
}
92+
```

outline.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ chapters:
3737
- errors
3838
- external-vocabularies
3939
- operation-path-naming
40+
- url-generation-strategy
4041
- extending-jsonld-context
4142
- identifiers
4243
- mongodb

0 commit comments

Comments
 (0)