wiki/content/20201109150109-subscribing_to_events_in_the_micro_kernel.md

1.1 KiB

date id title
20201109 6a54254e-2dae-47e5-8966-075e1bc1b7fa Subscribing to events in the micro kernel

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 ⚔'));
        }

        // ...
    }
}