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 trigger custom
9
- actions in your application. Those events are thrown by the HttpKernel
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
10
component and they are defined in the :class: `Symfony\\ Component\\ HttpKernel\\ KernelEvents `
11
11
class.
12
12
@@ -15,7 +15,7 @@ a service that listens to that event. As explained in this article, you can do
15
15
that in two different ways: creating an event listener or an event subscriber.
16
16
17
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
18
+ consistency purposes. In your own application, you can use any event and even mix
19
19
several of them in the same subscriber.
20
20
21
21
Creating an Event Listener
@@ -52,17 +52,14 @@ The most common way to listen to an event is to register an **event listener**::
52
52
$response->setStatusCode($exception->getStatusCode());
53
53
$response->headers->replace($exception->getHeaders());
54
54
} else {
55
- $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR );
55
+ $response->setStatusCode(500 );
56
56
}
57
57
58
58
// Send the modified response object to the event
59
59
$event->setResponse($response);
60
60
}
61
61
}
62
62
63
- .. versionadded :: 2.4
64
- Support for HTTP status code constants was introduced in Symfony 2.4.
65
-
66
63
.. tip ::
67
64
68
65
Each event receives a slightly different type of ``$event `` object. For
@@ -77,7 +74,7 @@ using a special "tag":
77
74
78
75
.. code-block :: yaml
79
76
80
- # app/config/config .yml
77
+ # app/config/services .yml
81
78
services :
82
79
kernel.listener.your_listener_name :
83
80
class : AppBundle\Listener\ExceptionListener
@@ -86,27 +83,27 @@ using a special "tag":
86
83
87
84
.. code-block :: xml
88
85
89
- <!-- app/config/config .xml -->
86
+ <!-- app/config/services .xml -->
90
87
<service id =" kernel.listener.your_listener_name" class =" AppBundle\Listener\ExceptionListener" >
91
88
<tag name =" kernel.event_listener" event =" kernel.exception" />
92
89
</service >
93
90
94
91
.. code-block :: php
95
92
96
- // app/config/config .php
93
+ // app/config/services .php
97
94
$container
98
95
->register('kernel.listener.your_listener_name', 'AppBundle\Listener\ExceptionListener')
99
96
->addTag('kernel.event_listener', array('event' => 'kernel.exception'))
100
97
;
101
98
102
99
.. note ::
103
100
104
- There is an optional tag option called ``method `` which defines which method
101
+ There is an optional tag attribute called ``method `` which defines which method
105
102
to execute when the event is triggered. By default the name of the method is
106
103
``on `` + "camel-cased event name". If the event is ``kernel.exception `` the
107
104
method executed by default is ``onKernelException() ``.
108
105
109
- The other optional tag option is called ``priority `` and it defaults to ``0 ``.
106
+ The other optional tag attribute is called ``priority `` which defaults to ``0 ``.
110
107
This value ranges from ``-255 `` to ``255 `` and it controls the order in which
111
108
listeners are executed (the highest the priority, the earlier a listener is
112
109
executed). This is useful when you need to guarantee that one listener is
@@ -126,8 +123,8 @@ listen to the same ``kernel.exception`` event::
126
123
namespace AppBundle\Subscriber;
127
124
128
125
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
129
- use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
130
126
use Symfony\Component\HttpFoundation\Response;
127
+ use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
131
128
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
132
129
133
130
class ExceptionSubscriber implements EventSubscriberInterface
@@ -169,7 +166,7 @@ is an event subscriber:
169
166
170
167
# app/config/config.yml
171
168
services :
172
- kernel.listener.your_subscriber_name :
169
+ app.exception_subscriber :
173
170
class : AppBundle\Subscriber\ExceptionSubscriber
174
171
tags :
175
172
- { name: kernel.event_subscriber }
@@ -181,7 +178,7 @@ is an event subscriber:
181
178
<container xmlns =" http://symfony.com/schema/dic/services" >
182
179
183
180
<services >
184
- <service id =" acme_exception_subscriber "
181
+ <service id =" app.exception_subscriber "
185
182
class =" AppBundle\Subscriber\ExceptionSubscriber" >
186
183
187
184
<tag name =" kernel.event_subscriber" />
@@ -195,7 +192,7 @@ is an event subscriber:
195
192
// app/config/config.php
196
193
$container
197
194
->register(
198
- 'acme_exception_subscriber ',
195
+ 'app.exception_subscriber ',
199
196
'AppBundle\Subscriber\ExceptionSubscriber'
200
197
)
201
198
->addTag('kernel.event_subscriber')
@@ -204,10 +201,6 @@ is an event subscriber:
204
201
Request Events, Checking Types
205
202
------------------------------
206
203
207
- .. versionadded :: 2.4
208
- The ``isMasterRequest() `` method was introduced in Symfony 2.4.
209
- Prior, the ``getRequestType() `` method must be used.
210
-
211
204
A single page can make several requests (one master request, and then multiple
212
205
sub-requests), which is why when working with the ``KernelEvents::REQUEST ``
213
206
event, you might need to check the type of the request. This can be easily
@@ -218,12 +211,13 @@ done as follow::
218
211
219
212
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
220
213
use Symfony\Component\HttpKernel\HttpKernel;
214
+ use Symfony\Component\HttpKernel\HttpKernelInterface;
221
215
222
216
class RequestListener
223
217
{
224
218
public function onKernelRequest(GetResponseEvent $event)
225
219
{
226
- if (! $event->isMasterRequest() ) {
220
+ if ($event->getRequestType() !== HttpKernelInterface::MASTER_REQUEST ) {
227
221
// don't do anything if it's not the master request
228
222
return;
229
223
}
@@ -237,5 +231,3 @@ done as follow::
237
231
Two types of request are available in the :class: `Symfony\\ Component\\ HttpKernel\\ HttpKernelInterface `
238
232
interface: ``HttpKernelInterface::MASTER_REQUEST `` and
239
233
``HttpKernelInterface::SUB_REQUEST ``.
240
-
241
- .. _`The EventDispatcher component` : http://symfony.com/doc/current/components/event_dispatcher/index.html
0 commit comments