Operation Path Naming

With API Platform Core, you can configure the default resolver used to generate operation paths. Pre-registered resolvers are available and can easily be overridden.

API Platform Core を使用すると、操作パスの生成に使用されるデフォルトのリゾルバーを構成できます。事前に登録されたリゾルバーが利用可能で、簡単にオーバーライドできます。

Configuration

There are two pre-registered operation path naming services:

事前に登録されている運用パスのネーミング サービスは次の 2 つです。

Service name Entity name Path result
api_platform.path_segment_name_generator.underscore MyResource /my_resources
api_platform.path_segment_name_generator.dash MyResource /my-resources

The default resolver is api_platform.path_segment_name_generator.underscore. To change it to the dash resolver, add the following lines to api/config/packages/api_platform.yaml:

デフォルトのリゾルバーは api_platform.path_segment_name_generator.underscore です。これをダッシュ​​ リゾルバーに変更するには、次の行を api/config/packages/api_platform.yaml に追加します。

# api/config/packages/api_platform.yaml
api_platform:
    path_segment_name_generator: api_platform.path_segment_name_generator.dash

Create a Custom Operation Path Resolver

Let's assume we need URLs without separators (e.g. api.tld/myresources)

区切り記号のない URL が必要だとしましょう (例: api.tld/myresources)。

Defining the Operation Segment Name Generator

Make sure the custom segment generator implements ApiPlatform\Operation\PathSegmentNameGeneratorInterface:

カスタム セグメント ジェネレーターが ApiPlatform\Operation\PathSegmentNameGeneratorInterface を実装していることを確認します。

<?php
// api/src/Operation/SingularPathSegmentNameGenerator.php
namespace App\Operation;

use ApiPlatform\Operation\PathSegmentNameGeneratorInterface;

class SingularPathSegmentNameGenerator implements PathSegmentNameGeneratorInterface
{
    /**
     * Transforms a given string to a valid path name which can be pluralized (eg. for collections).
     *
     * @param string $name usually a ResourceMetadata shortname
     *
     * @return string A string that is a part of the route name
     */
    public function getSegmentName(string $name, bool $collection = true): string
    {
        $name = $this->dashize($name);

        return $name;
    }

    private function dashize(string $string): string
    {
        return strtolower(preg_replace('~(?<=\\w)([A-Z])~', '-$1', $string));
    }
}

Note that $name contains a camel case string, by default the resource class name (e.g. MyResource).

$name にはキャメル ケースの文字列が含まれていることに注意してください。デフォルトでは、リソース クラス名 (例: MyResource) です。

Registering the Service

If you haven't disabled the autowiring option, the service will be registered automatically and you have nothing more to do. Otherwise, you must register this class as a service like in the following example:

自動配線オプションを無効にしていない場合、サービスは自動的に登録されるため、それ以上の作業はありません。それ以外の場合は、次の例のように、このクラスをサービスとして登録する必要があります。

# api/config/services.yaml
services:
    # ...
    'App\Operation\SingularPathSegmentNameGenerator': ~

Configuring It

# api/config/packages/api_platform.yaml
api_platform:
    path_segment_name_generator: 'App\Operation\SingularPathSegmentNameGenerator'