sajad torkamani

Diagram link

In a nutshell

The Symfony framework is essentially an HTTP request-response framework. While handling an incoming HTTP request, the framework dispatches some events that you can use to modify the request or response.

For example, Symfony dispatches several events during the lifecycle of HTTP requests:

  • When a request has been created.
  • When a controller action is about to be executed.
  • When a response is ready to be sent.
  • When an exception has been thrown.

You can create listeners that listen to such events and execute some logic. For example, you might create a listener that listens to exceptions and sends you an email when an exception is thrown.

Many Symfony components like Security, Messenger, Workflow, and Mailer use events extensively – either by dispatching their own events or listening to events from other Symfony components. Third-party packages or bundles can also dispatch their own events to make their code extensible.

Kernel events

Each event dispatched by the HttpKernel component is defined as a constant on the KernelEvents class. Each event listener is passed a single argument, which is some subclass of KernelEvent, which gives you the following information:

  • getRequestType() – Returns the type of the request (e.g., HttpKernelInterface::MAIN_REQUEST or HttpKernelInterface::SUB_REQUEST)
  • getKernel() – Returns the Kernel handling the request.
  • getRequest() – Returns the current Request being handled.
  • isMainRequest() – Checks if this is a main request.

How to create custom subscribers

Let’s create a subscriber that listens to the Symfony\Component\HttpKernel\Event\ControllerEvent event, which is dispatched just before the controller is called.

1. Generate a subscriber

symfony console make:subscriber ControllerEventSubscriber

The command will ask you what event you want to listen to. Enter the event name (e.g., Symfony\Component\HttpKernel\Event\ControllerEvent).

2. Implement subscriber logic

The make command will generate a subscriber file: src/EventSubscriber/ControllerEventSubscriber.php:

<?php

namespace App\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;

class ControllerEventSubscriber implements EventSubscriberInterface
{
    public function onControllerEvent(ControllerEvent $event): void
    {
        // do something...
    }

    public static function getSubscribedEvents(): array
    {
        return [
            ControllerEvent::class => 'onControllerEvent',
        ];
    }
}

Add your custom logic to the onControllerEvent method. You will have access to the event context (ControllerEvent).

List all registered event listeners

bin/console debug:event-dispatcher

List registered listeners for a particular event

bin/console debug:event-dispatcher <event-name>

For example, list all listeners for the kernel.controller event (see list of built-in Symfony events).

bin/console debug:event-dispatcher kernel.controller

Events reference

NameKernelEvents ConstantArgument passed to the listener
kernel.requestKernelEvents::REQUESTRequestEvent
kernel.controllerKernelEvents::CONTROLLERControllerEvent
kernel.controller_argumentsKernelEvents::CONTROLLER_ARGUMENTSControllerArgumentsEvent
kernel.viewKernelEvents::VIEWViewEvent
kernel.responseKernelEvents::RESPONSEResponseEvent
kernel.finish_requestKernelEvents::FINISH_REQUESTFinishRequestEvent
kernel.terminateKernelEvents::TERMINATETerminateEvent
kernel.exceptionKernelEvents::EXCEPTIONExceptionEvent

Sources

Tagged: Symfony

Leave a comment

Your email address will not be published. Required fields are marked *