php – Symfony 2.2扩展ExceptionController

前端之家收集整理的这篇文章主要介绍了php – Symfony 2.2扩展ExceptionController前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这个问题与 following change(Symfony 2.2版本的一部分)有关:

第1部分

在2.2之前的Symfony中,我重写了ExceptionController以显示一些自定义错误页面.

我这样做是通过:

parameters:
       twig.exception_listener.controller: My\CustomBundle\CustomExceptionController::showAction

现在,在升级到2.2后,我再也不能这样做,因为在生成异常时抛出异常(没有双关语):

ExceptionController::__construct() must be an instance of Twig_Environment,none given,called in…

由于ExceptionController现在是一个服务,我如何覆盖它,以及我需要在旧代码中更改什么?

我在自定义类中所做的全部更改了showAction方法中的模板引用:

$template = new TemplateReference('TwigBundle','Exception',$name,$format,'twig');

第2部分

由于ExceptionController不再扩展ContainerAware,我如何到达当前容器?是否足以实现ContainerAwareInterface?

你应该改变这几个:

>您需要在自定义Exception控制器中继承ExceptionController.
>您需要覆盖twig.controller.exception.class参数.正如您在the service file中看到的,它使用twig.controller.exception.class参数来标识异常控制器类.现在用你的类覆盖它:

parameters:
    twig.controller.exception.class: My\CustomBundle\CustomExceptionController

>您需要编辑showAction的签名以遵循new signature

Since ExceptionController no longer extends ContainerAware,how do I get to the current container? Is it enough to implement ContainerAwareInterface?

不,服务不应该永远不会注入容器.您应该在构造函数中注入所需的服务,就像使用Twig_Environment服务一样.

在您的Exception控制器中,您可以访问twig服务的$this-> twig属性.并且新签名获取$request参数,以获取请求.我认为你不需要更多. (你也得到$this-> debug)

原文链接:https://www.f2er.com/php/137796.html

猜你在找的PHP相关文章