【发布时间】:2020-07-09 19:21:43
【问题描述】:
当使用 symfony 实现 fos_rest 包时,在处理 404、405、500 或由 Symfony 触发的任何其他错误上的自定义错误页面时,我似乎无法获得 Symfony 的正常行为。
它适用于普通休息控制器中休息包触发的每个错误。
但是在我的登陆页面(以及关于我们等)上,它没有使用 fos_rest 包,而是使用 twig,自定义错误页面不起作用,相反,它似乎是由 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 行为?
【问题讨论】:
标签: php symfony symfony4 fosrestbundle