Skip to content

Commit a444951

Browse files
committed
Implemented the suggestions made by @xabbuh
1 parent 9a6dab7 commit a444951

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

cookbook/event_dispatcher/event_listener.rst

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
How to Create Event Listeners and Subscribers
66
=============================================
77

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
1010
component and they are defined in the :class:`Symfony\\Component\\HttpKernel\\KernelEvents`
1111
class.
1212

@@ -15,7 +15,7 @@ a service that listens to that event. As explained in this article, you can do
1515
that in two different ways: creating an event listener or an event subscriber.
1616

1717
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
1919
several of them in the same subscriber.
2020

2121
Creating an Event Listener
@@ -52,17 +52,14 @@ The most common way to listen to an event is to register an **event listener**::
5252
$response->setStatusCode($exception->getStatusCode());
5353
$response->headers->replace($exception->getHeaders());
5454
} else {
55-
$response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
55+
$response->setStatusCode(500);
5656
}
5757

5858
// Send the modified response object to the event
5959
$event->setResponse($response);
6060
}
6161
}
6262

63-
.. versionadded:: 2.4
64-
Support for HTTP status code constants was introduced in Symfony 2.4.
65-
6663
.. tip::
6764

6865
Each event receives a slightly different type of ``$event`` object. For
@@ -77,7 +74,7 @@ using a special "tag":
7774

7875
.. code-block:: yaml
7976
80-
# app/config/config.yml
77+
# app/config/services.yml
8178
services:
8279
kernel.listener.your_listener_name:
8380
class: AppBundle\Listener\ExceptionListener
@@ -86,27 +83,27 @@ using a special "tag":
8683
8784
.. code-block:: xml
8885
89-
<!-- app/config/config.xml -->
86+
<!-- app/config/services.xml -->
9087
<service id="kernel.listener.your_listener_name" class="AppBundle\Listener\ExceptionListener">
9188
<tag name="kernel.event_listener" event="kernel.exception" />
9289
</service>
9390
9491
.. code-block:: php
9592
96-
// app/config/config.php
93+
// app/config/services.php
9794
$container
9895
->register('kernel.listener.your_listener_name', 'AppBundle\Listener\ExceptionListener')
9996
->addTag('kernel.event_listener', array('event' => 'kernel.exception'))
10097
;
10198
10299
.. note::
103100

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
105102
to execute when the event is triggered. By default the name of the method is
106103
``on`` + "camel-cased event name". If the event is ``kernel.exception`` the
107104
method executed by default is ``onKernelException()``.
108105

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``.
110107
This value ranges from ``-255`` to ``255`` and it controls the order in which
111108
listeners are executed (the highest the priority, the earlier a listener is
112109
executed). This is useful when you need to guarantee that one listener is
@@ -126,8 +123,8 @@ listen to the same ``kernel.exception`` event::
126123
namespace AppBundle\Subscriber;
127124

128125
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
129-
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
130126
use Symfony\Component\HttpFoundation\Response;
127+
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
131128
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
132129

133130
class ExceptionSubscriber implements EventSubscriberInterface
@@ -169,7 +166,7 @@ is an event subscriber:
169166
170167
# app/config/config.yml
171168
services:
172-
kernel.listener.your_subscriber_name:
169+
app.exception_subscriber:
173170
class: AppBundle\Subscriber\ExceptionSubscriber
174171
tags:
175172
- { name: kernel.event_subscriber }
@@ -181,7 +178,7 @@ is an event subscriber:
181178
<container xmlns="http://symfony.com/schema/dic/services">
182179
183180
<services>
184-
<service id="acme_exception_subscriber"
181+
<service id="app.exception_subscriber"
185182
class="AppBundle\Subscriber\ExceptionSubscriber">
186183
187184
<tag name="kernel.event_subscriber"/>
@@ -195,7 +192,7 @@ is an event subscriber:
195192
// app/config/config.php
196193
$container
197194
->register(
198-
'acme_exception_subscriber',
195+
'app.exception_subscriber',
199196
'AppBundle\Subscriber\ExceptionSubscriber'
200197
)
201198
->addTag('kernel.event_subscriber')
@@ -204,10 +201,6 @@ is an event subscriber:
204201
Request Events, Checking Types
205202
------------------------------
206203

207-
.. versionadded:: 2.4
208-
The ``isMasterRequest()`` method was introduced in Symfony 2.4.
209-
Prior, the ``getRequestType()`` method must be used.
210-
211204
A single page can make several requests (one master request, and then multiple
212205
sub-requests), which is why when working with the ``KernelEvents::REQUEST``
213206
event, you might need to check the type of the request. This can be easily
@@ -218,12 +211,13 @@ done as follow::
218211

219212
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
220213
use Symfony\Component\HttpKernel\HttpKernel;
214+
use Symfony\Component\HttpKernel\HttpKernelInterface;
221215

222216
class RequestListener
223217
{
224218
public function onKernelRequest(GetResponseEvent $event)
225219
{
226-
if (!$event->isMasterRequest()) {
220+
if ($event->getRequestType() !== HttpKernelInterface::MASTER_REQUEST) {
227221
// don't do anything if it's not the master request
228222
return;
229223
}
@@ -237,5 +231,3 @@ done as follow::
237231
Two types of request are available in the :class:`Symfony\\Component\\HttpKernel\\HttpKernelInterface`
238232
interface: ``HttpKernelInterface::MASTER_REQUEST`` and
239233
``HttpKernelInterface::SUB_REQUEST``.
240-
241-
.. _`The EventDispatcher component`: http://symfony.com/doc/current/components/event_dispatcher/index.html

0 commit comments

Comments
 (0)