如何配置FOSRestBundle不干扰自定义异常控制器 [英] How to configure FOSRestBundle to not interfere with Custom Exception Controller

查看:119
本文介绍了如何配置FOSRestBundle不干扰自定义异常控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚将Symfony 2.7页面更新为2.8。除了Symfony本身,还更新了许多其他软件包。 FOSRestBundle 已从1.4版更新到2.1。

I have just updated my Symfony 2.7 page to 2.8. Beside Symfony itself a number of other packages have been updated as well. FOSRestBundle has been updated from version 1.4 to 2.1.

更新后, CustomExceptionController 我为 Twig 配置的功能不再起作用。诸如404或500之类的错误显示默认例外页面,而不是我的自定义页面。

After the update the CustomExceptionController I configured for Twig does not work any more. Error like 404 or 500 show the default exception page instead of my custom page.

这是配置:

// app/config/config.yml
...
twig:
    exception_controller:  app.exception_controller:showAction


// src/MyAppBundle/Resources/config/services.yml
app.exception_controller:
    class: MyAppBundle\Controller\CustomExceptionController
    arguments: ['@twig', '%kernel.debug%', "@translator.default" ]


// src/MyAppBundle/Controller/CustomExceptionController.php
use Symfony\Component\Debug\Exception\FlattenException;    

class CustomExceptionController extends ExceptionController {   

protected $translator;

public function __construct(\Twig_Environment $twig, $debug, Translator $translator) {
    parent::__construct($twig, $debug);
    $this->translator = $translator;
}

public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null) {
    ...
}

借助另一个线程我能够弄清楚,问题是由我根据 FOSRestBundle 更新说明

With the help of another thread I was able to figure out, that the problem is caused by a config change I made in accordance with the FOSRestBundle Update notes:

removed the ability of the AccessDeniedListener to render a response.
Use the FOSRestBundle or the twig exception controller in complement.
...
After:
     fos_rest:
        access_denied_listener: true
        exception: true # Activates the FOSRestBundle exception controller

遵循此提示和 Symfony文档,我在 config中添加了 fos_rest:exception:enabled:true 。 yml

Following this hint and the Symfony docs, I added fos_rest:exception:enabled: true to the config.yml:

// Before the Update:
fos_rest:
    ...
    access_denied_listener:
        json: true  
    exception:
        codes:
            'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
            'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
        messages:
            'Symfony\Component\Routing\Exception\ResourceNotFoundException': true

// After the Update:
fos_rest:
    ...
    access_denied_listener:
        json: true  
    exception:
        enabled: true  # <<< Added this line
        codes:
            'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
            'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
        messages:
            'Symfony\Component\Routing\Exception\ResourceNotFoundException': true

根据 Symfony文档,选项 fos_rest:exception:enabled:true 在版本1.4中已经必需,才能真正启用FOS异常控制器。因此,我不确定在更新之前配置是否正确以及FOS异常控制器是否按预期工作。

According to the Symfony docs, the option fos_rest:exception:enabled: true was already necessary in version 1.4 to actually enable the FOS Exception Controller. Thus I am not sure, if the configuration was correct before the update and if the FOS Exception Controller worked as intended.

但是,不必设置 fos_rest:exception:enabled:true 即可正确使用 access_denied_listener

However, not it is necessary to set fos_rest:exception:enabled: true to correctly use the access_denied_listener.

所以问题是:
access_denied_listener 应该可以工作正确(必须设置 fos_rest:exception:enabled:true ),并且我的 Twig 异常控制器仍应处理异常(如果设置 fos_rest:exception:enabled:true 不起作用)。

So the Problem is: access_denied_listener should be able to work properly (fos_rest:exception:enabled: true has to be set) and my Twig Exception Controller should still handle exceptions (does not work if fos_rest:exception:enabled: true is set).

如何正确执行设置/使用FOS异常控制器而不会干扰我的 Twig 异常控制器?

How do I properly set up / use the FOS Exception Controller without interfering with my Twig Exception Controller?

推荐答案

我认为答案可以在这里找到:

I think the answer can be found here:

https://symfony.com/doc/master/bundles/FOSRestBundle/4-exception-controller-support.html


FOSRestBundle定义了两个用于异常渲染的服务,默认情况下
配置fos_rest.exception.controller,仅支持
通过序列化器渲染。如果用户未配置显式控制器
并检测到TwigBundle,它将自动配置fos_rest.exception.twig_controller,其中
还支持通过Twig渲染。

FOSRestBundle defines two services for exception rendering, by default it configures fos_rest.exception.controller which only supports rendering via a serializer. In case no explicit controller is configured by the user and TwigBundle is detected it will automatically configure fos_rest.exception.twig_controller which additionally also supports rendering via Twig.

因此您需要扩展:
FOS\RestBundle\Controller\TwigExceptionController

So you need to extend: FOS\RestBundle\Controller\TwigExceptionController

这篇关于如何配置FOSRestBundle不干扰自定义异常控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆