Migrate From FOSRestBundle

FOSRestBundle is a popular bundle to rapidly develop RESTful APIs with Symfony. This page provides a guide to help developers migrating from FOSRestBundle to API Platform.

FOSRestBundle は、Symfony で RESTful API を迅速に開発するための人気のあるバンドルです。このページは、開発者が FOSRestBundle から API プラットフォームに移行するのに役立つガイドを提供します。

On 21 September, 2021, FOSRestBundle's creators recommended to use API Platform.

2021 年 9 月 21 日、FOSRestBundle の作成者は API プラットフォームの使用を推奨しました。

Features Comparison

The table below provides a list of the main features you can find in FOSRestBundle 3.1, and their equivalents in API Platform.

以下の表は、FOSRestBundle 3.1 で見つけることができる主な機能のリストと、API プラットフォームでの同等の機能を示しています。

Make CRUD endpoints

In FOSRestBundle

FOSRestBundle 内

Create a controller extending the AbstractFOSRestController abstract class, make your magic manually in your methods and return responses through the handleView() provided by FOSRest's ControllerTrait.

AbstractFOSRestController 抽象クラスを拡張するコントローラーを作成し、メソッドで手動でマジックを作成し、FOSRest の ControllerTrait によって提供される handleView() を介して応答を返します。

See The view layer.

ビューレイヤーを参照してください。

In API Platform

API プラットフォームで

Add the ApiResource attribute to your entities, and enable operations you desire inside. By default, every operations are activated.

エンティティに ApiResource 属性を追加し、内部で必要な操作を有効にします。デフォルトでは、すべての操作が有効になっています。

See Operations.

操作を参照してください。

Make custom controllers

In FOSRestBundle

FOSRestBundle 内

Same as above.

同上。

In API Platform

API プラットフォームで

Even though this is not recommended, API Platform allows you to create custom controllers and declare them in your entity's ApiResource attribute.

これは推奨されませんが、API プラットフォームでは、カスタム コントローラーを作成して、エンティティの ApiResource 属性で宣言することができます。

You can use them as you migrate from FOSRestBundle, but you should consider switching to Symfony Messenger as it will give you more benefits, such as compatibility with both REST and GraphQL, and better performances of your API on big tasks.

FOSRestBundle から移行するときにそれらを使用できますが、REST と GraphQL の両方との互換性や大きなタスクでの API のパフォーマンスの向上など、より多くの利点が得られるため、Symfony Messenger への切り替えを検討する必要があります。

See General Design Considerations.

一般的な設計上の考慮事項を参照してください。

Routing system (with native documentation support)

In FOSRestBundle

FOSRestBundle 内

Annotate your controllers with FOSRest's route annotations that are the most suitable to your needs.

ニーズに最も適した FOSRest のルート アノテーションを使用してコントローラーにアノテーションを付けます。

See Full default annotations.

完全なデフォルトの注釈を参照してください。

In API Platform

API プラットフォームで

Use the ApiResource attribute to activate the HTTP methods you need for your entity. By default, all the methods are enabled.

ApiResource 属性を使用して、エンティティに必要な HTTP メソッドをアクティブにします。デフォルトでは、すべてのメソッドが有効になっています。

See Operations.

操作を参照してください。

Hook into the requests handling

In FOSRestBundle

FOSRestBundle 内

Listen to FOSRest's events to modify the requests before they come into your controllers, and the responses after they come out of them.

FOSRest のイベントをリッスンして、要求がコントローラーに入る前に変更し、コントローラーから出た後の応答を変更します。

See Listener support.

リスナーのサポートを参照してください。

In API Platform

API プラットフォームで

API Platform provides a lot of ways to customize the behavior of your API, depending on what you exactly want to do.

API プラットフォームには、目的に応じて API の動作をカスタマイズする方法が多数用意されています。

See Extending API Platform for more details.

詳細については、API プラットフォームの拡張を参照してください。

Customize the formats of the requests and the responses

In FOSRestBundle

FOSRestBundle 内

Only the request body's format can be customized.

リクエストボディのフォーマットのみカスタマイズできます。

Use body listeners to use either FOSRest's own decoders or your own ones. FOSRestBundle provides native support for JSON and XML.

ボディ リスナーを使用して、FOSRest 独自のデコーダーまたは独自のデコーダーを使用します。 FOSRestBundle は、JSON と XML のネイティブ サポートを提供します。

See Body Listener.

ボディ リスナーを参照してください。

In API Platform

API プラットフォームで

Both the request and the response body's format can be customized.

要求と応答本文の両方の形式をカスタマイズできます。

You can configure the formats of the API either globally or in specific resources or operations. API Platform provides native support for multiple formats including JSON, XML, CSV, YAML, etc.

API の形式は、グローバルに、または特定のリソースまたは操作で構成できます。 API プラットフォームは、JSON、XML、CSV、YAML などの複数の形式をネイティブでサポートします。

See Content negociation.

コンテンツ ネゴシエーションを参照してください。

Name conversion

In FOSRestBundle

FOSRestBundle 内

Only request bodies can be converted before entering into your controller.

コントローラーに入る前に変換できるのは、リクエストボディのみです。

FOSRest provides two native normalizers for converting the names of your JSON keys to camelCase. You can create your own ones by implementing the ArrayNormalizerInterface.

FOSRest は、JSON キーの名前をキャメルケースに変換するための 2 つのネイティブ ノーマライザーを提供します。 ArrayNormalizerInterface を実装することで、独自のものを作成できます。

See Body Listeners.

ボディ リスナーを参照してください。

In API Platform

API プラットフォームで

Both request and response bodies can be converted.

リクエストボディとレスポンスボディの両方を変換できます。

API Platform uses name converters included in the Serializer component of Symfony. You can create your own by implementing the NameConverterInterface provided by Symfony.

API プラットフォームは、Symfony のシリアライザー コンポーネントに含まれる名前コンバーターを使用します。 Symfony が提供する NameConverterInterface を実装することで、独自のものを作成できます。

See Name Conversion in The Serialization Process.

シリアル化プロセスの名前変換を参照してください。

Handle errors

In FOSRestBundle

FOSRestBundle 内

Map the exceptions to HTTP statuses in the fos_rest.exception parameter.

fos_rest.exception パラメーターで例外を HTTP ステータスにマップします。

See ExceptionController support.

ExceptionController のサポートを参照してください。

In API Platform

API プラットフォームで

Map the exceptions to HTTP statuses in the api_platform.exception_to_status parameter.

api_platform.exception_to_status パラメータで例外を HTTP ステータスにマップします。

See Errors Handling.

エラー処理を参照してください。

Security

In FOSRestBundle

FOSRestBundle 内

Use Symfony's Security component to control your API access.

Symfony のセキュリティ コンポーネントを使用して、API アクセスを制御します。

In API Platform

API プラットフォームで

Use the security attribute in the ApiResource and ApiProperty attributes. It is an Expression language string describing who can access your resources or who can see the properties of your resources. By default, everything is accessible without authentication.

ApiResource および ApiProperty 属性で security 属性を使用します。これは、誰がリソースにアクセスできるか、または誰がリソースのプロパティを表示できるかを記述する式言語文字列です。デフォルトでは、認証なしですべてにアクセスできます。

Note you can also use the security.yml file if you only need to limit access to specific roles.

特定のロールへのアクセスのみを制限する必要がある場合は、security.yml ファイルを使用することもできます。

See Security.

セキュリティを参照してください。

API versioning

In FOSRestBundle

FOSRestBundle 内

FOSRestBundle provides a way to provide versions to your APIs in a way users have to specify which one they want to use.

FOSRestBundle は、ユーザーが使用するバージョンを指定する必要がある方法で、API にバージョンを提供する方法を提供します。

See API versioning.

API のバージョン管理を参照してください。

In API Platform

API プラットフォームで

API Platform has no native support to API versioning, but instead provides an approach consisting of deprecating resources when needed. It allows a smoother upgrade for clients, as they need to change their code only when it is necessary.

API プラットフォームには、API のバージョニングに対するネイティブ サポートはありませんが、代わりに、必要に応じてリソースを非推奨にするアプローチを提供します。必要な場合にのみコードを変更する必要があるため、クライアントはよりスムーズにアップグレードできます。

See Deprecating Resources and Properties.

リソースとプロパティの非推奨を参照してください。