如何配置 FOSRestBundle 使其不干扰自定义异常控制器
How to configure FOSRestBundle to not interfere with Custom Exception Controller
我刚刚将我的 Symfony 2.7 页面更新到 2.8。除了 Symfony 本身,许多其他软件包也已更新。 FOSRestBundle
已从版本 1.4 更新到 2.1。
更新后,我为 Twig
配置的 CustomExceptionController
不再起作用。 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) {
...
}
在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 docs,我将 fos_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
根据 Symfony docs,选项 fos_rest:exception:enabled: true
在版本 1.4 中已经是实际启用 FOS 异常控制器所必需的。因此,我不确定更新前配置是否正确以及 FOS 异常控制器是否按预期工作。
但是,并不是必须设置fos_rest:exception:enabled: true
才能正确使用access_denied_listener
。
所以问题是:
access_denied_listener
应该能够正常工作(fos_rest:exception:enabled: true
必须设置)并且我的 Twig
异常控制器应该仍然处理异常(如果设置 fos_rest:exception:enabled: true
则不起作用)。
如何在不干扰我的 Twig
异常控制器的情况下正确设置/使用 FOS 异常控制器?
我想答案可以在这里找到:
https://symfony.com/doc/master/bundles/FOSRestBundle/4-exception-controller-support.html
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
我刚刚将我的 Symfony 2.7 页面更新到 2.8。除了 Symfony 本身,许多其他软件包也已更新。 FOSRestBundle
已从版本 1.4 更新到 2.1。
更新后,我为 Twig
配置的 CustomExceptionController
不再起作用。 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) {
...
}
在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 docs,我将 fos_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
根据 Symfony docs,选项 fos_rest:exception:enabled: true
在版本 1.4 中已经是实际启用 FOS 异常控制器所必需的。因此,我不确定更新前配置是否正确以及 FOS 异常控制器是否按预期工作。
但是,并不是必须设置fos_rest:exception:enabled: true
才能正确使用access_denied_listener
。
所以问题是:
access_denied_listener
应该能够正常工作(fos_rest:exception:enabled: true
必须设置)并且我的 Twig
异常控制器应该仍然处理异常(如果设置 fos_rest:exception:enabled: true
则不起作用)。
如何在不干扰我的 Twig
异常控制器的情况下正确设置/使用 FOS 异常控制器?
我想答案可以在这里找到:
https://symfony.com/doc/master/bundles/FOSRestBundle/4-exception-controller-support.html
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