URL Generation Strategy

By default, API Platform generates all URLs as absolute paths to the base URL.

デフォルトでは、API プラットフォームはすべての URL をベース URL への絶対パスとして生成します。

For instance, in JSON-LD, you will get a collection like this:

たとえば、JSON-LD では、次のようなコレクションが得られます。

{
  "@context": "/contexts/Book",
  "@id": "/books",
  "@type": "hydra:Collection",
  "hydra:member": [
    {
      "@id": "/books/1",
      "@type": "https://schema.org/Book",
      "name": "My awesome book"
    }
  ],
  "hydra:totalItems": 1
}

You may want to use absolute URLs (for instance if resources are used in another API) or network paths instead.

代わりに、絶対 URL (たとえば、リソースが別の API で使用されている場合) またはネットワーク パスを使用することができます。

It can be configured globally:

グローバルに構成できます。

# api/config/packages/api_platform.yaml
api_platform:
    defaults:
        url_generation_strategy: !php/const ApiPlatform\Api\UrlGeneratorInterface::ABS_URL

It can also be configured only for a specific resource:

特定のリソースに対してのみ構成することもできます。

[codeSelector]

[コードセレクター]

<?php
// api/src/Entity/Book.php

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Api\UrlGeneratorInterface;

#[ApiResource(urlGenerationStrategy: UrlGeneratorInterface::ABS_URL)]
class Book
{
    // ...
}
# api/config/api_platform/resources.yaml
App\Entity\Book:
    urlGenerationStrategy: !php/const ApiPlatform\Api\UrlGeneratorInterface::ABS_URL
<?xml version="1.0" encoding="UTF-8" ?>
<!-- api/config/api_platform/resources.xml -->

<resources
        xmlns="https://api-platform.com/schema/metadata/resources-3.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="https://api-platform.com/schema/metadata/resources-3.0
        https://api-platform.com/schema/metadata/resources-3.0.xsd">
    <resource class="App\Entity\Book" urlGenerationStrategy="0" />
</resources>

[/codeSelector]

[/コードセレクター]

For the above configuration, the collection will be like this:

上記の構成の場合、コレクションは次のようになります。

{
  "@context": "http://example.com/contexts/Book",
  "@id": "http://example.com/books",
  "@type": "hydra:Collection",
  "hydra:member": [
    {
      "@id": "http://example.com/books/1",
      "@type": "https://schema.org/Book",
      "name": "My awesome book"
    }
  ],
  "hydra:totalItems": 1
}