I’ve implemented the “How to create an Event Listener“ to catch & handle all exceptions throw by my app.
But, when I go to the home page, I got this errors (from the apache logs):
In production mode (app.php):
syntax error, unexpected ‘-‘, expecting ‘(‘ appProdProjectContainer.php line 383
In dev. mode (app_dev.php):
syntax error, unexpected ‘-‘, expecting ‘(‘ in appDevDebugProjectContainer.php on line 1131
I’ve googled, but I didn’t find something that can solve my issue…
So, here, it’s my services.yml loaded by the “DependencyInjection/lhnbackendExtension” class:
services:
kernel.listener.lhn-exception:
class: lhn\backendBundle\Listener\LhnExceptionListener
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
–> I’ve tried to configure the service in a XML file, but the same error occurs…
And the basic listener implementation:
namespace lhn\backendBundle\Listener;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;
class LhnExceptionListener
{
public function onKernelException(GetResponseForExceptionEvent $event){
// We get the exception object from the received event
$exception = $event->getException();
$message = 'My Error says: ' . $exception->getMessage();
// Customize our response object to display our exception details
$response = new Response();
$response->setContent($message);
$response->setStatusCode($exception->getStatusCode());
// Send our modified response object to the event
$event->setResponse($response);
}
}
Try to go without hypen in the service name.
Becomes: