2015-04-05 136 views
1

我试图在symfony2中生成一个自定义错误页面: 我正在关注此tuto。 但是它表明我:自定义错误页面InvalidArgumentException 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 services.yml. Check your YAML syntax. 

我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); 
    } 
} 
} 

任何想法可能是什么?当他在那里时,我完全遵循。

回答

1
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文件,你缺少的“类”,“论据” &“标签”缩进级别。他们似乎是在同一水平,你的服务名“kernel.listener.allotaxi_exception_listener”

看一看上面的服务定义,注意服务名称和类,参数&标签之间的压痕。

在处理yml文件时,需要注意正确的层次结构

+0

是的,就是这样。谢谢 – 2015-04-06 23:38:37