Extending API Platform

Because it handles the complex, tedious and repetitive task of creating an API infrastructure for you, API Platform lets you focus on what matter the most for the end user: the business logic. To do so, API Platform provides a lot of extension points you can use to hook your own code. Those extensions points are taken into account both by the REST and GraphQL subsystems.

API インフラストラクチャを作成するという複雑で退屈で反復的なタスクを処理するため、API プラットフォームを使用すると、エンド ユーザーにとって最も重要なことであるビジネス ロジックに集中できます。これらの拡張ポイントは、REST サブシステムと GraphQL サブシステムの両方で考慮されます。

The following tables summarizes which extension point to use depending on what you want to do:


Extension Point Usage
State Providers adapters for custom persistence layers, virtual fields, custom hydration
Denormalizers post-process objects created from the payload sent in the HTTP request body
Voters custom authorization logic
Validation constraints custom validation logic
State Processors custom business logic and computations to trigger before or after persistence (ex: mail, call to an external API...)
Normalizers customize the resource sent to the client (add fields in JSON documents, encode codes, dates...)
Filters create filters for collections and automatically document them (OpenAPI, GraphQL, Hydra)
Serializer Context Builders change the Serialization context (e.g. groups) dynamically
Messenger Handlers create 100% custom, RPC, async, service-oriented endpoints (should be used in place of custom controllers because the messenger integration is compatible with both REST and GraphQL, while custom controllers only work with REST)
DTOs use a specific class to represent the input or output data structure related to an operation
Kernel Events customize the HTTP request or response (REST only, other extension points must be preferred when possible)

Doctrine Specific Extension Points

Extension Point Usage
Extensions Access to the query builder to change the DQL query
Filters Add filters documentations (OpenAPI, GraphQL, Hydra) and automatically apply them to the DQL query

Leveraging the Built-in Infrastructure Using Composition

While most API Platform classes are marked as final, built-in services are straightforward to reuse and customize using composition.

ほとんどの API プラットフォーム クラスは final としてマークされていますが、組み込みサービスは構成を使用して簡単に再利用およびカスタマイズできます。

For instance, if you want to send a mail after a resource has been persisted, but still want to benefit from the native Doctrine ORM state processor, use the decorator design pattern to wrap the native state processor in your own class sending the mail, as demonstrated in this example.

たとえば、リソースが永続化された後にメールを送信したいが、それでもネイティブの Doctrine ORM ステート プロセッサを利用したい場合は、デコレータ デザイン パターンを使用して、メールを送信する独自​​のクラスでネイティブ ステート プロセッサをラップします。この例で示されています。

To replace existing API Platform services with your decorators, check out how to decorate services.

既存の API プラットフォーム サービスをデコレーターに置き換えるには、サービスを装飾する方法を確認してください。

Service Decoration screencast
Watch the Service Decoration screencast