Symfony 如何配置FOSRestBundle以不干扰自定义异常控制器
我刚刚将我的Symfony 2.7页面更新为2.8。除了Symfony本身之外,还更新了许多其他软件包Symfony 如何配置FOSRestBundle以不干扰自定义异常控制器,symfony,exception-handling,fosrestbundle,Symfony,Exception Handling,Fosrestbundle,我刚刚将我的Symfony 2.7页面更新为2.8。除了Symfony本身之外,还更新了许多其他软件包FOSRestBundle已从版本1.4更新为2.1 更新后,为Twig配置的CustomExceptionControllerI不再工作。404或500等错误显示默认异常页面,而不是我的自定义页面 以下是配置: // app/config/config.yml ... twig: exception_controller: app.exception_controller:showA
FOSRestBundle
已从版本1.4更新为2.1
更新后,为Twig
配置的CustomExceptionController
I不再工作。404或500等错误显示默认异常页面,而不是我的自定义页面
以下是配置:
// 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
进行的配置更改引起的:
在这个提示和之后,我将fos\u rest:exception:enabled:true
添加到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
//更新之前:
fos_休息:
...
访问被拒绝\u侦听器:
json:true
例外情况:
代码:
'Symfony\Component\Routing\Exception\ResourceNotFoundException':404
“条令\ORM\OptimisticLockException”:HTTP\U冲突
信息:
“Symfony\Component\Routing\Exception\ResourceNotFoundException”:true
//更新后:
fos_休息:
...
访问被拒绝\u侦听器:
json:true
例外情况:
启用:正确#我想答案可以在这里找到:
默认情况下,FOSRestBundle为异常呈现定义了两个服务
它配置fos_rest.exception.controller,该控制器仅支持
通过序列化程序进行渲染。在没有显式控制器的情况下
由用户配置,并检测到TwigBundle,它将
自动配置fos_rest.exception.twig_控制器
此外,还支持通过细枝进行渲染
因此,您需要扩展:
FOS\RestBundle\Controller\TwigeExceptionController
// 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