【问题标题】:Custom error pages not working when exception controller is enabled启用异常控制器时自定义错误页面不起作用
【发布时间】: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


    【解决方案1】:

    我找到了使用区域的解决方案。我必须在我的 fos_rest.yaml 中添加以下代码

    zone:
        - { path: '^/myROUTE1' }
        - { path: '^/myROUTE2' }
        - { path: '^/myROUTE3' }
        - { path: '^/myROUTE4' }
    

    here所述

    希望这会有所帮助。

    【讨论】:

    • 谢谢,正是我所需要的,一直在错误地查看异常控制器支持部分。我原以为配置参考中会提到 zone。
    猜你喜欢
    • 2019-03-04
    • 1970-01-01
    • 2019-01-02
    • 2013-03-06
    • 2012-07-24
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多