@@ -6,23 +6,25 @@ How to Create Event Listeners and Subscribers
6
6
=============================================
7
7
8
8
Symfony has various events and hooks that can be used to trigger custom
9
- behavior in your application. Those events are thrown by the HttpKernel
10
- component and can be viewed in the :class: `Symfony\\ Component\\ HttpKernel\\ KernelEvents ` class.
9
+ actions in your application. Those events are thrown by the HttpKernel
10
+ component and they are defined in the :class: `Symfony\\ Component\\ HttpKernel\\ KernelEvents `
11
+ class.
11
12
12
- To hook into an event and add your own custom logic, you have to create
13
- a service that listens to that event. You can do that in two different ways,
14
- creating an event listener or an event subscriber instead. In this entry,
15
- you will see the two ways of creating a service that will act as an exception
16
- listener, allowing you to modify how exceptions are shown by your application.
17
- The ``KernelEvents::EXCEPTION `` event is just one of the core kernel events.
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.
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.
18
20
19
21
Creating an Event Listener
20
22
--------------------------
21
23
22
- The most common way to listen to an event is to register an event listener::
24
+ The most common way to listen to an event is to register an ** event listener ** ::
23
25
24
- // src/AppBundle/EventListener /ExceptionListener.php
25
- namespace AppBundle\EventListener ;
26
+ // src/AppBundle/Listener /ExceptionListener.php
27
+ namespace AppBundle\Listener ;
26
28
27
29
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
28
30
use Symfony\Component\HttpFoundation\Response;
@@ -78,44 +80,50 @@ using a special "tag":
78
80
# app/config/config.yml
79
81
services :
80
82
kernel.listener.your_listener_name :
81
- class : AppBundle\EventListener\AcmeExceptionListener
83
+ class : AppBundle\Listener\ExceptionListener
82
84
tags :
83
- - { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
85
+ - { name: kernel.event_listener, event: kernel.exception }
84
86
85
87
.. code-block :: xml
86
88
87
89
<!-- app/config/config.xml -->
88
- <service id =" kernel.listener.your_listener_name" class =" AppBundle\EventListener\AcmeExceptionListener " >
89
- <tag name =" kernel.event_listener" event =" kernel.exception" method = " onKernelException " />
90
+ <service id =" kernel.listener.your_listener_name" class =" AppBundle\Listener\ExceptionListener " >
91
+ <tag name =" kernel.event_listener" event =" kernel.exception" />
90
92
</service >
91
93
92
94
.. code-block :: php
93
95
94
96
// app/config/config.php
95
97
$container
96
- ->register('kernel.listener.your_listener_name', 'AppBundle\EventListener\AcmeExceptionListener ')
97
- ->addTag('kernel.event_listener', array('event' => 'kernel.exception', 'method' => 'onKernelException' ))
98
+ ->register('kernel.listener.your_listener_name', 'AppBundle\Listener\ExceptionListener ')
99
+ ->addTag('kernel.event_listener', array('event' => 'kernel.exception'))
98
100
;
99
101
100
102
.. note ::
101
103
102
- There is an additional tag option ``priority `` that is optional and defaults
103
- to 0. This value can be from -255 to 255, and the listeners will be executed
104
- in the order of their priority (highest to lowest). This is useful when
105
- you need to guarantee that one listener is executed before another.
104
+ There is an optional tag option called ``method `` which defines which method
105
+ to execute when the event is triggered. By default the name of the method is
106
+ ``on `` + "camel-cased event name". If the event is ``kernel.exception `` the
107
+ method executed by default is ``onKernelException() ``.
108
+
109
+ The other optional tag option is called ``priority `` and it defaults to ``0 ``.
110
+ This value ranges from ``-255 `` to ``255 `` and it controls the order in which
111
+ listeners are executed (the highest the priority, the earlier a listener is
112
+ executed). This is useful when you need to guarantee that one listener is
113
+ executed before another.
106
114
107
115
Creating an Event Subscriber
108
116
----------------------------
109
117
110
- Another way to listen to events is via an event subscriber. An event subscriber
111
- can define one or various methods that listen to one or various events,
112
- and can set a priority for each method. The higher the priority, the earlier
113
- the method is called. To learn more about event subscribers, see :doc: `/components/event_dispatcher/introduction `.
114
- The following example shows a subscriber that subscribes various methods
115
- to the ``kernel.exception `` event::
118
+ Another way to listen to events is via an ** event subscriber **, which is a class
119
+ that can define one or more methods that listen to one or various events. The
120
+ event priority can be defined for each method (the higher the priority, the earlier
121
+ the method is called) . To learn more about event subscribers, read :doc: `/components/event_dispatcher/introduction `.
122
+ The following example shows an event subscriber that defines several methods which
123
+ listen to the same ``kernel.exception `` event::
116
124
117
- // src/AppBundle/EventListener /ExceptionSubscriber.php
118
- namespace AppBundle\EventSubscriber ;
125
+ // src/AppBundle/Subscriber /ExceptionSubscriber.php
126
+ namespace AppBundle\Subscriber ;
119
127
120
128
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
121
129
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
@@ -126,45 +134,27 @@ to the ``kernel.exception`` event::
126
134
{
127
135
public static function getSubscribedEvents()
128
136
{
129
- // Return the events it is subscribed to, the methods that listen each event and the
130
- // priority of each method
137
+ // return the subscribed events, their methods and priorities
131
138
return array(
132
139
'kernel.exception' => array(
133
- array('onKernelExceptionPre ', 10),
134
- array('onKernelExceptionMid ', 5 ),
135
- array('onKernelExceptionPost ', 0 ),
140
+ array('processException ', 10),
141
+ array('logException ', 0 ),
142
+ array('notifyException ', -10 ),
136
143
)
137
144
);
138
145
}
139
146
140
- public function onKernelExceptionPre (GetResponseForExceptionEvent $event)
147
+ public function processException (GetResponseForExceptionEvent $event)
141
148
{
142
- $exception = $event->getException();
143
- $message = sprintf(
144
- 'My Error says: %s with code: %s',
145
- $exception->getMessage(),
146
- $exception->getCode()
147
- );
148
-
149
- $response = new Response();
150
- $response->setContent($message);
151
-
152
- if ($exception instanceof HttpExceptionInterface) {
153
- $response->setStatusCode($exception->getStatusCode());
154
- $response->headers->replace($exception->getHeaders());
155
- } else {
156
- $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
157
- }
158
-
159
- $event->setResponse($response);
149
+ // ...
160
150
}
161
151
162
- public function onKernerlExceptionMid (GetResponseForExceptionEvent $event)
152
+ public function logException (GetResponseForExceptionEvent $event)
163
153
{
164
154
// ...
165
155
}
166
156
167
- public function onKernerlExceptionPost (GetResponseForExceptionEvent $event)
157
+ public function notifyException (GetResponseForExceptionEvent $event)
168
158
{
169
159
// ...
170
160
}
@@ -180,7 +170,7 @@ is an event subscriber:
180
170
# app/config/config.yml
181
171
services :
182
172
kernel.listener.your_subscriber_name :
183
- class : AppBundle\EventSubscriber\AcmeExceptionSubscriber
173
+ class : AppBundle\Subscriber\ExceptionSubscriber
184
174
tags :
185
175
- { name: kernel.event_subscriber }
186
176
@@ -192,7 +182,7 @@ is an event subscriber:
192
182
193
183
<services >
194
184
<service id =" acme_exception_subscriber"
195
- class =" AppBundle\EventSubscriber\AcmeExceptionSubscriber " >
185
+ class =" AppBundle\Subscriber\ExceptionSubscriber " >
196
186
197
187
<tag name =" kernel.event_subscriber" />
198
188
@@ -206,7 +196,7 @@ is an event subscriber:
206
196
$container
207
197
->register(
208
198
'acme_exception_subscriber',
209
- 'AppBundle\EventSubscriber\AcmeExceptionSubscriber '
199
+ 'AppBundle\Subscriber\ExceptionSubscriber '
210
200
)
211
201
->addTag('kernel.event_subscriber')
212
202
;
@@ -223,13 +213,13 @@ sub-requests), which is why when working with the ``KernelEvents::REQUEST``
223
213
event, you might need to check the type of the request. This can be easily
224
214
done as follow::
225
215
226
- // src/AppBundle/EventListener/AcmeRequestListener .php
227
- namespace AppBundle\EventListener ;
216
+ // src/AppBundle/Listener/RequestListener .php
217
+ namespace AppBundle\Listener ;
228
218
229
219
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
230
220
use Symfony\Component\HttpKernel\HttpKernel;
231
221
232
- class AcmeRequestListener
222
+ class RequestListener
233
223
{
234
224
public function onKernelRequest(GetResponseEvent $event)
235
225
{
0 commit comments