自定义错误页InvalidArgumentException Symfony2
我正在尝试在symfony2中生成自定义错误页: 我一直在关注这个。 然而,它告诉我:自定义错误页InvalidArgumentException Symfony2,symfony,Symfony,我正在尝试在symfony2中生成自定义错误页: 我一直在关注这个。 然而,它告诉我: 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 servi
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);
}
}
}
知道会是什么吗?他到那里的时候我就跟着他
services:
kernel.listener.allotaxi_exception_listener:
class: AlloTaxi\AlloTaxiBundle\Listener\ExceptionListener
arguments: [@templating, @kernel]
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
通过查看问题中发布的services.yml文件,您缺少“class”、“arguments”和“tags”的缩进级别。它们似乎与服务名“kernel.listener.allotaxi\u exception\u listener”处于同一级别
看看上面的服务定义,注意服务名称和类、参数和标记之间的缩进
在处理yml文件时,需要注意适当的层次结构是的,就是这样。谢谢