Introduction

From Symfony 4.0 it’s possible to subscribe to events using the EventSubscriberInterface Interface

Syntax

// src/Kernel.php
namespace App;
 
use App\Exception\DangerException;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\HttpKernel\KernelEvents;
 
class Kernel extends BaseKernel implements EventSubscriberInterface
{
    use MicroKernelTrait;
 
    // ...
 
    public static function getSubscribedEvents()
    {
        return [KernelEvents::EXCEPTION => 'handleExceptions'];
    }
 
    public function handleExceptions(GetResponseForExceptionEvent $event)
    {
        if ($event->getException() instanceof DangerException) {
            $event->setResponse(Response::create('It\'s dangerous to go alone. Take this ⚔'));
        }
 
        // ...
    }
}