One of the most powerful features of Symfony Framework is its Event Driven Architecture. It is very easy and simple to implement PHP logic that responds to certain events during the request - response cycle.
Events related to the Symfony Kernel
Kernel Request Event
use Symfony\Component\HttpKernel\Event\RequestEvent;
class MyRequestListener
{
public function onKernelRequest(RequestEvent $event)
{
// Perform logic before handling the request
// Example: Set a custom request attribute
$request = $event->getRequest();
$request->attributes->set('custom_attribute', 'some_value');
}
}
Kernel View Event
use Symfony\Component\HttpKernel\Event\ViewEvent;
class MyViewListener
{
public function onKernelView(ViewEvent $event)
{
// Perform logic after the controller has been executed
// Example: Modify the response
$response = $event->getResponse();
$response->setContent('Modified content');
}
}
Kernel Controller Event
use Symfony\Component\HttpKernel\Event\ControllerEvent;
class MyControllerListener
{
public function onKernelController(ControllerEvent $event)
{
// Perform logic before the controller is executed
// Example: Modify the controller arguments
$controller = $event->getController();
$arguments = $event->getArguments();
// Modify the arguments array as needed
$event->setArguments($arguments);
}
}
Kernel Response Event
use Symfony\Component\HttpKernel\Event\ResponseEvent;
class MyResponseListener
{
public function onKernelResponse(ResponseEvent $event)
{
// Perform logic before sending the response
// Example: Add a custom header to the response
$response = $event->getResponse();
$response->headers->set('X-Custom-Header', 'some_value');
}
}
Kernel Exception Event
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
class MyExceptionListener
{
public function onKernelException(ExceptionEvent $event)
{
// Perform logic when an exception is thrown
// Example: Log the exception
$exception = $event->getThrowable();
// Log the exception using your preferred logger
}
}
To register any of these listeners, you can use the kernel.event_listener
or kernel.event_subscriber
tags in your config/services.yaml
file, similar to the previous example. For example:
services:
App\EventListener\MyRequestListener:
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
App\EventListener\MyViewListener:
tags:
- { name: kernel.event_listener, event: kernel.view, method: onKernelView }
App\EventListener\MyControllerListener:
tags:
- { name: kernel.event_listener, event: kernel.controller, method: onKernelController }
App\EventListener\MyResponseListener:
tags:
- { name: kernel.event_listener, event: kernel.response, method: onKernelResponse }
App\EventListener\MyExceptionListener:
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
Custom Events
Create the Event Class
First, you need to create the event class that represents the event you want to listen to. For example, let's create a UserRegisteredEvent
class
<?php
namespace App\Event;
use Symfony\Contracts\EventDispatcher\Event;
class UserRegisteredEvent extends Event
{
protected $user;
public function __construct($user)
{
$this->user = $user;
}
public function getUser()
{
return $this->user;
}
}
Create the Event Listener
Next, you need to create the event listener class that will handle the event. Let's create a UserRegisteredListener
class
<?php
namespace App\EventListener;
use App\Event\UserRegisteredEvent;
class UserRegisteredListener
{
public function onUserRegistered(UserRegisteredEvent $event)
{
$user = $event->getUser();
// Perform your logic here, such as sending an email, updating the database, etc.
// Example: Send a welcome email to the user
$email = $user->getEmail();
// Code to send the email...
}
}
Register the Event Listener
To register the event listener, you need to configure it in your Symfony application. Open the config/services.yaml
file and add the following configuration
services:
App\EventListener\UserRegisteredListener:
tags:
- { name: kernel.event_listener, event: 'user.registered', method: 'onUserRegistered' }
Dispatch the Event
Finally, you need to dispatch the event from the appropriate location in your code. For example, when a user registers, you can dispatch the UserRegisteredEvent
<?php
namespace App\Controller;
use App\Event\UserRegisteredEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class RegistrationController
{
/**
* @Route("/register", name="register")
*/
public function register(EventDispatcherInterface $eventDispatcher): Response
{
// User registration logic...
// Dispatch the UserRegisteredEvent
$user = ...; // Retrieve the user object
$event = new UserRegisteredEvent($user);
$eventDispatcher->dispatch($event, 'user.registered');
return new Response('Registration successful!');
}
}
Whenever a user is registered and the UserRegisteredEvent
is dispatched, the UserRegisteredListener::onUserRegistered()
method will be called, allowing you to perform any necessary actions in response to the event.
For more details on this topic, go to the Symfony Official Documentation: Events and Listeners