<?php
/**
* @author <akartis-dev>
*/
namespace App\EventListener;
use ApiPlatform\Core\Api\UrlGeneratorInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class ExceptionEventListener
{
public function __construct(
private UrlGeneratorInterface $urlGenerator,
)
{
}
/**
* Handle user access denied
*
* @param ExceptionEvent $event
* @return void
*/
public function onKernelException(ExceptionEvent $event)
{
$indexPath = $this->urlGenerator->generate('app_index', ['_locale' => 'fr']);
if ($event->getThrowable() instanceof AccessDeniedHttpException) {
$event->setResponse(new RedirectResponse($indexPath));
}
}
}