|
5 | 5 | How to Create Event Listeners and Subscribers
|
6 | 6 | =============================================
|
7 | 7 |
|
8 |
| -Symfony has various events and hooks that can be used to perform custom |
9 |
| -actions in your application. Those events are triggered by the HttpKernel |
10 |
| -component and they are defined in the :class:`Symfony\\Component\\HttpKernel\\KernelEvents` |
11 |
| -class. |
| 8 | +During the execution of a Symfony application, lots of event notifications are |
| 9 | +triggered. Your application can listen to these notifications and respond to |
| 10 | +them by executing any piece of code. |
12 | 11 |
|
13 |
| -To hook into an event and execute your own custom logic, you have to create |
14 |
| -a service that listens to that event. As explained in this article, you can do |
15 |
| -that in two different ways: creating an event listener or an event subscriber. |
| 12 | +Internal events provided by Symfony itself are defined in the |
| 13 | +:class:`Symfony\\Component\\HttpKernel\\KernelEvents` class. Third-party bundles |
| 14 | +and libraries also trigger lots of events and your own application can trigger |
| 15 | +:doc:`custom events </components/event_dispatcher/index>`. |
16 | 16 |
|
17 |
| -The examples of this article only use the ``KernelEvents::EXCEPTION`` event for |
18 |
| -consistency purposes. In your own application, you can use any event and even mix |
19 |
| -several of them in the same subscriber. |
| 17 | +All the examples shown in this article use the same ``KernelEvents::EXCEPTION`` |
| 18 | +event for consistency purposes. In your own application, you can use any event |
| 19 | +and even mix several of them in the same subscriber. |
20 | 20 |
|
21 | 21 | Creating an Event Listener
|
22 | 22 | --------------------------
|
@@ -107,8 +107,8 @@ using a special "tag":
|
107 | 107 | ``0`` and it controls the order in which listeners are executed (the highest
|
108 | 108 | the priority, the earlier a listener is executed). This is useful when you
|
109 | 109 | need to guarantee that one listener is executed before another. The priorities
|
110 |
| - of the internal Symfony events range from ``-255`` to ``255`` but your own |
111 |
| - events can use any positive or negative integer. |
| 110 | + of the internal Symfony listeners usually range from ``-255`` to ``255`` but |
| 111 | + your own listeners can use any positive or negative integer. |
112 | 112 |
|
113 | 113 | Creating an Event Subscriber
|
114 | 114 | ----------------------------
|
@@ -182,14 +182,15 @@ is an event subscriber:
|
182 | 182 |
|
183 | 183 | <!-- app/config/services.xml -->
|
184 | 184 | <?xml version="1.0" encoding="UTF-8" ?>
|
185 |
| - <container xmlns="http://symfony.com/schema/dic/services"> |
| 185 | + <container xmlns="http://symfony.com/schema/dic/services" |
| 186 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 187 | + xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> |
186 | 188 |
|
187 | 189 | <services>
|
188 | 190 | <service id="app.exception_subscriber"
|
189 | 191 | class="AppBundle\Subscriber\ExceptionSubscriber">
|
190 | 192 |
|
191 | 193 | <tag name="kernel.event_subscriber"/>
|
192 |
| -
|
193 | 194 | </service>
|
194 | 195 | </services>
|
195 | 196 | </container>
|
|
0 commit comments