Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Updating the HttpKernel event classes #11898

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions event_dispatcher.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ The most common way to listen to an event is to register an **event listener**::
namespace App\EventListener;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

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

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

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

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class ExceptionSubscriber implements EventSubscriberInterface
Expand All @@ -168,17 +168,17 @@ listen to the same ``kernel.exception`` event::
];
}

public function processException(GetResponseForExceptionEvent $event)
public function processException(ExceptionEvent $event)
{
// ...
}

public function logException(GetResponseForExceptionEvent $event)
public function logException(ExceptionEvent $event)
{
// ...
}

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

.. tip::

Since Symfony 4.3 you can subscribe to events using the FQCN of the event.
For example ``ExceptionEvent::class`` instead of ``KernelEvents::EXCEPTION``.
This allows you to develop code based on pure PHP classes instead of inventing arbitrary strings to name events.

.. _ref-event-subscriber-configuration:

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

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;

class RequestListener
{
public function onKernelRequest(GetResponseEvent $event)
public function onKernelRequest(RequestEvent $event)
{
if (!$event->isMasterRequest()) {
// don't do anything if it's not the master request
Expand Down