Reputation: 446
I'm trying to generate a custom error page in symfony2: I was following this tuto. However it shows me this:
InvalidArgumentException in YamlFileLoader.php line 145: A service definition must be an array or a string starting with "@" but NULL found for service "kernel.listener.allotaxi_exception_listener" in services.yml. Check your YAML syntax.
my services.yml :
services:
kernel.listener.allotaxi_exception_listener:
class: AlloTaxi\AlloTaxiBundle\Listener\ExceptionListener
arguments: [@templating, @kernel]
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
ExceptionListener.php :
namespace AlloTaxi\AlloTaxiBundle\Listener;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
class ExceptionListener {
protected $templating;
protected $kernel;
public function __construct(EngineInterface $templating, $kernel)
{
$this->templating = $templating;
$this->kernel = $kernel;
}
public function onKernelException(GetResponseForExceptionEvent $event)
{
// provide the better way to display a enhanced error page only in prod environment, if you want
if ('prod' == $this->kernel->getEnvironment()) {
// exception object
$exception = $event->getException();
// new Response object
$response = new Response();
// set response content
$response->setContent(
// create you custom template AcmeFooBundle:Exception:exception.html.twig
$this->templating->render(
'AlloTaxiBundle:Exception:error.html.twig',
array('exception' => $exception)
)
);
// HttpExceptionInterface is a special type of exception
// that holds status code and header details
if ($exception instanceof HttpExceptionInterface) {
$response->setStatusCode($exception->getStatusCode());
$response->headers->replace($exception->getHeaders());
} else {
$response->setStatusCode(500);
}
// set the new $response object to the $event
$event->setResponse($response);
}
}
}
Any idea what could it be? I followed exactly when he did there.
Upvotes: 1
Views: 1572
Reputation: 2279
services:
kernel.listener.allotaxi_exception_listener:
class: AlloTaxi\AlloTaxiBundle\Listener\ExceptionListener
arguments: [@templating, @kernel]
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
By looking at the services.yml file posted in your question, you are missing the indentation level for "class", "arguments" & "tags". They appear to be at the same level as that of your service name "kernel.listener.allotaxi_exception_listener"
Have a look at above service definition, notice the indentation between service name and class, arguments & tags.
When dealing with yml files one needs to be careful about proper hierarchy
Upvotes: 1