I use Symfony 7.0 framework. I got stuck with this for a few days. Googled, used AI chat, read documentation, but nothing helped.
I can’t get use details inside of my Event Listener class from Security component. This code:
<?php
namespace AppEventListener;
use SymfonyComponentEventDispatcherAttributeAsEventListener;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentHttpKernelEventExceptionEvent;
use SymfonyComponentHttpKernelExceptionNotFoundHttpException;
use SymfonyComponentHttpKernelKernelEvents;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyBundleSecurityBundleSecurity;
final class ExceptionListener extends AbstractController
{
public function __construct(
protected Security $security,
)
{
}
#[AsEventListener(event: KernelEvents::EXCEPTION)]
public function onKernelException(ExceptionEvent $event): void
{
// Getting exception from ExceptionEvent object
$exception = $event->getThrowable();
// Getting current user
$user = $this->security->getUser();
die(var_export($user));
// Provera da li je izuzetak tipa NotFoundHttpException
if ($exception instanceof NotFoundHttpException) {
// Odredjivanje template-a u zavisnosti od uloge korisnika
$template = 'error404_user.html.twig'; // Default template za ulogovane korisnike
$roles = [];
// Provera da li je korisnik prijavljen i koje uloge ima
if ($user) {
$roles = $user->getRoles();
if (in_array('ROLE_ADMIN', $roles)) {
$template = 'error404_admin.html.twig'; // Template za admina
}
}
// Generisanje odgovora sa odgovarajućim template-om
$response = new Response();
$response->setContent(
$this->render($template, ['exception' => $exception, 'roles' => $roles])->getContent()
);
// Postavljanje HTTP status koda na 404
$response->setStatusCode(Response::HTTP_NOT_FOUND);
// Postavljanje odgovora u ExceptionEvent objekat
$event->setResponse($response);
}
}
}
This is service.yaml
code:
# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters:
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
AppEventListenerExceptionListener:
tags: [kernel.event_listener]
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones
die(var_export($user));
always backs NULL
.
I want to get user details inside of event listener. Depending of user role, the user should be redirected to an appropriate page. Can someone help me with this?