You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: core/events.md
+65-52Lines changed: 65 additions & 52 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,9 +9,70 @@ of event listeners are executed which validate the data, persist it in database,
9
9
and create an HTTP response that will be sent to the client.
10
10
11
11
To do so, API Platform Core leverages [events triggered by the Symfony HTTP Kernel](https://symfony.com/doc/current/reference/events.html#kernel-events).
12
-
You can also hook your own code to those events. They are handy and powerful extension points available at all points
12
+
You can also hook your own code to those events. There are handy and powerful extension points available at all points
13
13
of the request lifecycle.
14
14
15
+
If you are using Doctrine, lifecycle events ([ORM](https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/events.html#lifecycle-events), [MongoDB ODM](https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/events.html#lifecycle-events))
16
+
are also available if you want to hook into the persistence layer's object lifecycle.
17
+
18
+
## Built-in Event Listeners
19
+
20
+
These built-in event listeners are registered for routes managed by API Platform:
21
+
22
+
Name | Event | [Pre & Post hooks](#custom-event-listeners) | Priority | Description
`AddFormatListener` | `kernel.request` | None | 7 | Guesses the best response format ([content negotiation](content-negotiation.md))
25
+
`ReadListener` | `kernel.request` | `PRE_READ`, `POST_READ` | 4 | Retrieves data from the persistence system using the [data providers](data-providers.md) (`GET`, `PUT`, `DELETE`)
26
+
`DeserializeListener` | `kernel.request` | `PRE_DESERIALIZE`, `POST_DESERIALIZE` | 2 | Deserializes data into a PHP entity (`GET`, `POST`, `DELETE`); updates the entity retrieved using the data provider (`PUT`)
`WriteListener` | `kernel.view` | `PRE_WRITE`, `POST_WRITE` | 32 | Persists changes in the persistence system using the [data persisters](data-persisters.md) (`POST`, `PUT`, `DELETE`)
30
+
`SerializeListener` | `kernel.view` | `PRE_SERIALIZE`, `POST_SERIALIZE` | 16 | Serializes the PHP entity in string [according to the request format](content-negotiation.md)
31
+
`RespondListener` | `kernel.view` | `PRE_RESPOND`, `POST_RESPOND` | 8 | Transforms serialized to a `Symfony\Component\HttpFoundation\Response` instance
32
+
`AddLinkHeaderListener` | `kernel.response` | None | 0 | Adds a `Link` HTTP header pointing to the Hydra documentation
33
+
`ValidationExceptionListener` | `kernel.exception` | None | 0 | Serializes validation exceptions in the Hydra format
34
+
`ExceptionListener` | `kernel.exception` | None | -96 | Serializes PHP exceptions in the Hydra format (including the stack trace in debug mode)
35
+
36
+
Some of these built-in listeners can be enabled/disabled by setting operation attributes:
37
+
38
+
Attribute | Type | Default | Description
39
+
--------------|--------|---------|-------------
40
+
`read` | `bool` | `true` | Enables or disables `ReadListener`
41
+
`deserialize` | `bool` | `true` | Enables or disables `DeserializeListener`
42
+
`validate` | `bool` | `true` | Enables or disables `ValidateListener`
43
+
`write` | `bool` | `true` | Enables or disables `WriteListener`
44
+
`serialize` | `bool` | `true` | Enables or disables `SerializeListener`
45
+
46
+
Some of these built-in listeners can be enabled/disabled by setting request attributes (for instance in the [`defaults`
47
+
attribute of an operation](operations.md#recommended-method)):
`_api_persist` | `bool` | `true` | Enables or disables `WriteListener`
54
+
55
+
## Custom Event Listeners
56
+
57
+
Registering your own event listeners to add extra logic is convenient.
58
+
59
+
The [`ApiPlatform\Core\EventListener\EventPriorities`](https://github.com/api-platform/core/blob/master/src/EventListener/EventPriorities.php) class comes with a convenient set of class constants corresponding to commonly used priorities:
Alternatively, [the subscriber must be registered manually](http://symfony.com/doc/current/components/http_kernel/introduction.html#creating-an-event-listener).
`AddFormatListener` | `kernel.request` | None | 7 | Guesses the best response format ([content negotiation](content-negotiation.md))
79
-
`ReadListener` | `kernel.request` | `PRE_READ`, `POST_READ` | 4 | Retrieves data from the persistence system using the [data providers](data-providers.md) (`GET`, `PUT`, `DELETE`)
80
-
`DeserializeListener` | `kernel.request` | `PRE_DESERIALIZE`, `POST_DESERIALIZE`| 2 | Deserializes data into a PHP entity (`GET`, `POST`, `DELETE`); updates the entity retrieved using the data provider (`PUT`)
`WriteListener` | `kernel.view` | `PRE_WRITE`, `POST_WRITE` | 32 | Persists changes in the persistence system using the [data persisters](data-persisters.md) (`POST`, `PUT`, `DELETE`)
83
-
`SerializeListener` | `kernel.view` | `PRE_SERIALIZE`, `POST_SERIALIZE` | 16 | Serializes the PHP entity in string [according to the request format](content-negotiation.md)
84
-
`RespondListener` | `kernel.view` | `PRE_RESPOND`, `POST_RESPOND` | 8 | Transforms serialized to a `Symfony\Component\HttpFoundation\Response` instance
85
-
`AddLinkHeaderListener` | `kernel.response` | None | 0 | Adds a `Link` HTTP header pointing to the Hydra documentation
86
-
`ValidationExceptionListener` | `kernel.exception` | None | 0 | Serializes validation exceptions in the Hydra format
87
-
`ExceptionListener` | `kernel.exception` | None | -96 | Serializes PHP exceptions in the Hydra format (including the stack trace in debug mode)
88
-
89
-
Those built-in listeners are always executed for routes managed by API Platform. Registering your own event listeners to
90
-
add extra logic is convenient.
91
-
92
-
The [`ApiPlatform\Core\EventListener\EventPriorities`](https://github.com/api-platform/core/blob/master/src/EventListener/EventPriorities.php) class comes with a convenient set of class constants corresponding to commonly used priorities:
Some of those built-in listeners can be enabled/disabled by setting request attributes ([for instance in the `defaults`
110
-
attribute of an operation](operations.md#recommended-method)):
126
+
If you use the official API Platform distribution, creating the previous class is enough. The Symfony DependencyInjection
127
+
component will automatically register this subscriber as a service and will inject its dependencies thanks to the [autowiring feature](https://symfony.com/doc/current/service_container/autowiring.html).
@@ -142,59 +140,24 @@ that handles the file upload.
142
140
143
141
namespace App\Controller;
144
142
145
-
use ApiPlatform\Core\Bridge\Symfony\Validator\Exception\ValidationException;
146
-
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
147
-
use ApiPlatform\Core\Util\RequestAttributesExtractor;
148
-
use ApiPlatform\Core\Validator\ValidatorInterface;
149
143
use App\Entity\MediaObject;
150
-
use Doctrine\Common\Persistence\ManagerRegistry;
151
144
use Symfony\Component\HttpFoundation\Request;
152
145
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
153
146
154
147
final class CreateMediaObjectAction
155
148
{
156
-
private $managerRegistry;
157
-
private $validator;
158
-
private $resourceMetadataFactory;
159
-
160
-
public function __construct(ManagerRegistry $managerRegistry, ValidatorInterface $validator, ResourceMetadataFactoryInterface $resourceMetadataFactory)
Everything is fully customizable through a powerful event system and strong OOP.
40
+
Everything is fully customizable through a powerful [event system](events.md) and strong OOP.
41
41
42
42
This bundle is extensively tested (unit and functional). The [`Fixtures/` directory](https://github.com/api-platform/core/tree/master/tests/Fixtures) contains a working app covering all features of the library.
This way, it will skip the `Read`, `Deserialize` and `Validate` listeners (see [the event system](events.md) for more
814
-
information).
810
+
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)
0 commit comments