【发布时间】:2014-08-13 10:23:47
【问题描述】:
我正在尝试覆盖默认的异常控制器。我尝试遵循官方 Symfony 2 食谱以及 this advice。
我做的步骤:
-
在我的包 (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')); } } -
在我放置的
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%]
我还有一个自定义的 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 更改为其他?)还是问题出在其他地方?
【问题讨论】:
-
试试:
class: \EdProfileBundle\Controller\MyExceptionController -
我收到以下错误:\EdProfileBundle\\Controller\\MyExceptionController'" 不是“twig.controller.exception”服务的有效类名
-
您在
Controller课程中缺少您的namespace。将namespace EdProfileBundle\Controller;添加到文件顶部。 -
Ferdynator:感谢您的建议,但我收到以下错误:致命错误:在 (...)\app\cache\prod\appProdDebugProjectContainer.php 中找不到类 'EdProfileBundle\Controller\MyExceptionController'在线 2951