Symfony 2.2扩展了ExceptionController [英] Symfony 2.2 extend ExceptionController
问题描述
此问题与以下更改(Symfony 2.2版本的一部分)相关:
This question is related to the following change (part of Symfony 2.2 release):
第1部分
在2.2之前的Symfony中,我覆盖 ExceptionController
显示一些自定义的错误页面。
In pre-2.2 Symfony, I was overriding ExceptionController
to display some custom error pages.
我是通过以下方式进行的:
I did that via:
parameters:
twig.exception_listener.controller: My\CustomBundle\CustomExceptionController::showAction
现在,升级到2.2后,我不能再这样做了,因为在生成异常时抛出异常(没有双关语):
Now, after upgrading to 2.2, I can no longer do that, because an exception is thrown while generating an exception (no pun intended):
ExceptionController :: __ construct()必须是Twig_Environment的一个实例,没有给定,在...中调用
ExceptionController::__construct() must be an instance of Twig_Environment, none given, called in...
由于 ExceptionController
现在是一项服务,我该如何覆盖呢? ge在我的旧代码中?
Since ExceptionController
is a service now, how can I override that, and what do I need to change in my old code?
我在自定义类中所做的所有改变了showAction方法中的模板引用:
All I did in the custom class, is changed the template reference in showAction method:
$template = new TemplateReference('TwigBundle', 'Exception', $name, $format, 'twig');
第2部分
由于 ExceptionController
不再扩展 ContainerAware
,如何获取当前容器?是否足够实现 ContainerAwareInterface
?
Since ExceptionController
no longer extends ContainerAware
, how do I get to the current container? Is it enough to implement ContainerAwareInterface
?
推荐答案
这个:
- 你需要继承
ExceptionController
您的自定义异常控制器。 -
您需要覆盖
twig.controller.exception.class
参数即可。您可以在服务文件中看到,它使用twig.controller.exception.class
参数来标识异常控制器类。现在用你的类覆盖它:
- you need to inherit the
ExceptionController
in your custom Exception controller. you need to override the
twig.controller.exception.class
parameter. As you can see in the service file, it uses thetwig.controller.exception.class
parameter to identify the exception controller class. Now override it with your class:
parameters:
twig.controller.exception.class: My\CustomBundle\CustomExceptionController
您需要编辑 showAction
遵循新签名
由于
ExceptionController
不再扩展ContainerAware
如何到达当前的容器?是否足够实现ContainerAwareInterface
?
Since
ExceptionController
no longer extendsContainerAware
, how do I get to the current container? Is it enough to implementContainerAwareInterface
?
不,服务不应该不要注入容器。您应该在构造函数中注入所需的服务,就像使用 Twig_Environment
服务一样。
No, services shouldn't never inject the container. You should inject the services you need in the constructor, as is done with the Twig_Environment
service.
控制器,您可以访问twig服务的 $ this-> twig
属性。并且新的签名获得 $ request
参数,以获取请求。我不认为你需要更多。 (你也得到 $ this-> debug
)
Inside your Exception controller, you get access to the $this->twig
property for the twig service. And the new signature gets a $request
parameter, to get the request. I don't think you need more. (you also get $this->debug
)
这篇关于Symfony 2.2扩展了ExceptionController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!