mirror of
https://github.com/alrayyes/wiki.git
synced 2024-11-25 04:56:23 +00:00
1.1 KiB
1.1 KiB
id | title |
---|---|
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 ⚔'));
}
// ...
}
}