首页 > 解决方案 > 启用异常控制器时自定义错误页面不起作用

问题描述

当使用 symfony 实现 fos_rest 包时,在处理 404、405、500 或由 Symfony 触发的任何其他错误上的自定义错误页面时,我似乎无法获得 Symfony 的正常行为。

它适用于普通休息控制器中休息包触发的每个错误。

但是在我的登陆页面(以及关于我们等)上,它没有使用 fos_rest 包,而是使用 twig,自定义错误页面不起作用,相反,它似乎由 fos_rest 包处理,并且总是发送默认错误 500(即使它应该触发 404 错误)。

如果我在 fos_rest.yaml文件enabled: false

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 包以仅处理由我的休息控制器处理的路由的异常,并为站点的其余部分保留正常的 Symfony 4 行为?

标签: phpsymfonysymfony4fosrestbundle

解决方案


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

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

如此处所述

希望这可以帮助。


推荐阅读