[当用symfony实现fos_rest捆绑软件时,在处理404、405、500或Symfony触发的任何其他错误上的自定义错误页面时,我似乎没有Symfony的正常行为。
对于正常的休息控制器中的休息包触发的每个错误,它都可以正常工作。
但是在我的登录页面(以及关于我们的页面等等)上,该页面未使用fos_rest捆绑软件,而是使用细枝,自定义错误页面不起作用,相反,它似乎仍由fos_rest捆绑软件处理,并且总是发送默认错误500(即使它应该触发404错误)。
如果我停用fos_rest.yaml文件(enabled: false
)中的异常,那么,自定义错误页面可以正常工作(在此文档以下https://symfony.com/doc/4.4/controller/error_pages.html进行配置)
fos_rest:
routing_loader:
default_format: json
include_format: false
body_listener: true
format_listener:
rules:
- { path: '^/myROUTE1', priorities: ['json'], fallback_format: json, prefer_extension: false }
- { path: '^/myROUTE2', priorities: ['json'], fallback_format: json, prefer_extension: false }
- { path: '^/myROUTE3', priorities: ['json'], fallback_format: json, prefer_extension: false }
- { path: '^/myROUTE4', priorities: ['json'], fallback_format: json, prefer_extension: false }
- { path: '^/', priorities: ['html', 'json'], fallback_format: 'html' }
param_fetcher_listener: true
access_denied_listener:
json: true
view:
view_response_listener: 'force'
formats:
json: true
exception:
enabled: true
exception_controller: 'fos_rest.exception.controller:showAction'
codes:
Doctrine\ORM\EntityNotFoundException: 404
\LogicException: 400
\DomainException: 400
messages:
Doctrine\ORM\EntityNotFoundException: true
\LogicException: true
\DomainException: true
我如何设置fos_rest捆绑包,使其仅处理由我的rest控制器处理的路由的异常,并保留其余站点的正常Symfony 4行为?
我通过使用区域找到了解决方案。我必须在我的fos_rest.yaml中添加以下代码]
zone: - { path: '^/myROUTE1' } - { path: '^/myROUTE1' } - { path: '^/myROUTE1' } - { path: '^/myROUTE1' }
如here中所述
希望这会有所帮助。