|
| 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 | +``` |
0 commit comments