无法覆盖Symfony2中的默认异常控制器

时间:2014-08-13 10:23:48

标签: php symfony

我试图覆盖默认的异常控制器。我试图遵循官方的Symfony 2食谱和this advice

我做的步骤:

  1. 在我的软件包(Ed)中,我创建了名为MyExceptionController.php的新控制器文件:

    <?php
    use Symfony\Bundle\TwigBundle\Controller\ExceptionController;
    
    class MyExceptionController extends ExceptionController 
    {
    
        //override the showAction() or findTemplate() method here
        public function showAction($token)
        {   
            if (null === $this->profiler) {
                throw new NotFoundHttpException('The profiler must be enabled.');
            }
    
            $this->profiler->disable();
    
            $exception = $this->profiler->loadProfile($token)->getCollector('exception')->getException();
            $template = $this->getTemplate();
    
            if (!$this->twig->getLoader()->exists($template)) {
                $handler = new ExceptionHandler();
    
                return new Response($handler->getContent($exception), 200, array('Content-Type' => 'text/html'));
            }
    
            $code = $exception->getStatusCode();
            $name = "Mark";
            return new Response($this->twig->render(
                $template,
                array(
                    'status_code'    => $code,
                    'status_text'    => Response::$statusTexts[$code],
                    'exception'      => $exception,
                    'logger'         => null,
                    'currentContent' => '',
                    'name' => $name,
                )
            ), 200, array('Content-Type' => 'text/html'));
        }
    }
    
  2. 在我放置的config.yml文件中:

    twig:
        debug:            "%kernel.debug%"
        strict_variables: "%kernel.debug%"
        autoescape: false
        exception_controller:  twig.controller.exception:showAction
    
    services:
        twig.controller.exception:
            class: EdProfileBundle\Controller\MyExceptionController
            arguments: [@twig, %kernel.debug%]
    
  3. 我还有一个自定义的exception.html.twig页面,在我覆盖异常控制器之前工作正常。现在,当异常发生时,我收到以下错误:

    Fatal error: Class 'EdProfileBundle\Controller\MyExceptionController' not found in (..)\app\cache\prod\appProdDebugProjectContainer.php on line 2951
    

    config.yml中的名称/位置有问题(例如:我是否必须将twig.controller.exception更改为其他?)或者问题出在其他地方?

1 个答案:

答案 0 :(得分:0)

尝试使用arguments: ["@twig", "%kernel.debug%"]代替arguments: [@twig, %kernel.debug%]

相关问题