|
1 |
| -# Creating Custom Operations and Controllers |
| 1 | +# Creating Custom Operations and Symfony Controllers |
2 | 2 |
|
3 |
| -Note: using custom controllers with API Platform is **discouraged**. Also, GraphQL is **not supported**. |
4 |
| -[For most use cases, better extension points, working both with REST and GraphQL, are available](design.md). |
| 3 | +> [!NOTE] |
| 4 | +> Using custom Symfony controllers with API Platform is **discouraged**. Also, GraphQL is **not supported**. |
| 5 | +> [For most use cases, better extension points, working both with REST and GraphQL, are available](../core/design.md). |
| 6 | +> We recommend to use [System providers and processors](../core/extending.md#system-providers-and-processors) to extend API Platform internals. |
5 | 7 |
|
6 | 8 | API Platform can leverage the Symfony routing system to register custom operations related to custom controllers. Such custom
|
7 | 9 | controllers can be any valid [Symfony controller](https://symfony.com/doc/current/controller.html), including standard
|
8 |
| -Symfony controllers extending the [`Symfony\Bundle\FrameworkBundle\Controller\AbstractController`](http://api.symfony.com/4.1/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.html) |
| 10 | +Symfony controllers extending the [`Symfony\Bundle\FrameworkBundle\Controller\AbstractController`](https://symfony.com/doc/current/controller.html#the-base-controller-class-services) |
9 | 11 | helper class.
|
10 | 12 |
|
11 | 13 | To enable this feature use `use_symfony_listeners: true` in your `api_platform` configuration file:
|
@@ -39,7 +41,7 @@ If your resource has any identifier, this operation will look like `/books/{id}`
|
39 | 41 | Those routes are not exposed from any documentation (for instance OpenAPI), but are anyway declared on the Symfony routing and always return a HTTP 404.
|
40 | 42 |
|
41 | 43 | If you create a custom operation, you will probably want to properly document it.
|
42 |
| -See the [OpenAPI](openapi.md) part of the documentation to do so. |
| 44 | +See the [OpenAPI](../core/openapi.md) part of the documentation to do so. |
43 | 45 |
|
44 | 46 | First, let's create your custom operation:
|
45 | 47 |
|
@@ -90,7 +92,7 @@ you need and it will be autowired too.
|
90 | 92 | The `__invoke` method of the action is called when the matching route is hit. It can return either an instance of
|
91 | 93 | `Symfony\Component\HttpFoundation\Response` (that will be displayed to the client immediately by the Symfony kernel) or,
|
92 | 94 | like in this example, an instance of an entity mapped as a resource (or a collection of instances for collection operations).
|
93 |
| -In this case, the entity will pass through [all built-in event listeners](events.md#built-in-event-listeners) of API Platform. It will be |
| 95 | +In this case, the entity will pass through [all built-in event listeners](../core/events.md#built-in-event-listeners) of API Platform. It will be |
94 | 96 | automatically validated, persisted and serialized in JSON-LD. Then the Symfony kernel will send the resulting document to
|
95 | 97 | the client.
|
96 | 98 |
|
@@ -165,7 +167,7 @@ Complex use cases may lead you to create multiple custom operations.
|
165 | 167 |
|
166 | 168 | In such a case, you will probably create the same amount of custom controllers while you may not need to perform custom logic inside.
|
167 | 169 |
|
168 |
| -To avoid that, API Platform provides the `ApiPlatform\Action\PlaceholderAction` which behaves the same when using the [built-in operations](operations.md#operations). |
| 170 | +To avoid that, API Platform provides the `ApiPlatform\Action\PlaceholderAction` which behaves the same when using the [built-in operations](../core/operations.md#operations). |
169 | 171 |
|
170 | 172 | You just need to set the `controller` attribute with this class. Here, the previous example updated:
|
171 | 173 |
|
@@ -370,7 +372,7 @@ resources:
|
370 | 372 |
|
371 | 373 | </code-selector>
|
372 | 374 |
|
373 |
| -This way, it will skip the `ReadListener`. You can do the same for some other built-in listeners. See [Built-in Event Listeners](events.md#built-in-event-listeners) |
| 375 | +This way, it will skip the `ReadListener`. You can do the same for some other built-in listeners. See [Built-in Event Listeners](../core/events.md#built-in-event-listeners) |
374 | 376 | for more information.
|
375 | 377 |
|
376 | 378 | In your custom controller, the `__invoke()` method parameter should be called the same as the entity identifier.
|
|
0 commit comments