Skip to content

Commit 9a6a7f2

Browse files
authored
Updating the HttpKernel event classes
Updating the 4.3 documentation for the event dispatcher
1 parent c19d405 commit 9a6a7f2

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

event_dispatcher.rst

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ The most common way to listen to an event is to register an **event listener**::
2727
namespace App\EventListener;
2828

2929
use Symfony\Component\HttpFoundation\Response;
30-
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
30+
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
3131
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
3232

3333
class ExceptionListener
3434
{
35-
public function onKernelException(GetResponseForExceptionEvent $event)
35+
public function onKernelException(ExceptionEvent $event)
3636
{
3737
// You get the exception object from the received event
3838
$exception = $event->getException();
@@ -63,7 +63,7 @@ The most common way to listen to an event is to register an **event listener**::
6363
.. tip::
6464

6565
Each event receives a slightly different type of ``$event`` object. For
66-
the ``kernel.exception`` event, it is :class:`Symfony\\Component\\HttpKernel\\Event\\GetResponseForExceptionEvent`.
66+
the ``kernel.exception`` event, it is :class:`Symfony\\Component\\HttpKernel\\Event\\ExceptionEvent`.
6767
Check out the :doc:`Symfony events reference </reference/events>` to see
6868
what type of object each event provides.
6969

@@ -151,7 +151,7 @@ listen to the same ``kernel.exception`` event::
151151
namespace App\EventSubscriber;
152152

153153
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
154-
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
154+
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
155155
use Symfony\Component\HttpKernel\KernelEvents;
156156

157157
class ExceptionSubscriber implements EventSubscriberInterface
@@ -168,17 +168,17 @@ listen to the same ``kernel.exception`` event::
168168
];
169169
}
170170

171-
public function processException(GetResponseForExceptionEvent $event)
171+
public function processException(ExceptionEvent $event)
172172
{
173173
// ...
174174
}
175175

176-
public function logException(GetResponseForExceptionEvent $event)
176+
public function logException(ExceptionEvent $event)
177177
{
178178
// ...
179179
}
180180

181-
public function notifyException(GetResponseForExceptionEvent $event)
181+
public function notifyException(ExceptionEvent $event)
182182
{
183183
// ...
184184
}
@@ -187,6 +187,12 @@ listen to the same ``kernel.exception`` event::
187187
That's it! Your ``services.yaml`` file should already be setup to load services from
188188
the ``EventSubscriber`` directory. Symfony takes care of the rest.
189189

190+
.. tip::
191+
192+
Since Symfony 4.3 you can subscribe to events using the FQCN of the event.
193+
For example ``ExceptionEvent::class`` instead of ``KernelEvents::EXCEPTION``.
194+
This allows you to develop code based on pure PHP classes instead of inventing arbitrary strings to name events.
195+
190196
.. _ref-event-subscriber-configuration:
191197

192198
.. tip::
@@ -207,11 +213,11 @@ or a "sub request"::
207213
// src/EventListener/RequestListener.php
208214
namespace App\EventListener;
209215

210-
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
216+
use Symfony\Component\HttpKernel\Event\RequestEvent;
211217

212218
class RequestListener
213219
{
214-
public function onKernelRequest(GetResponseEvent $event)
220+
public function onKernelRequest(RequestEvent $event)
215221
{
216222
if (!$event->isMasterRequest()) {
217223
// don't do anything if it's not the master request

0 commit comments

Comments
 (0)