symfony2 从 onKernelRequest 抛出异常返回空白页

标签 symfony exception request event-listener custom-error-pages

我有一个连接到 kernel.request 事件的事件监听器。在 onKernel 请求方法中,我基于子域进行一些验证。 如果此验证失败,我想抛出一些异常,例如 AccessDenied 或其他异常。

问题是当我抛出异常时,它显示一个空白页面而不是我的自定义错误页面。

如果我检查 prod.log 文件,我会得到以下信息:

[2013-06-06 11:08:38] request.ERROR: Exception thrown when handling an exception (Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException: This route needs to be accessed with a subdomain) [] []
[2013-06-06 11:16:32] request.ERROR: Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException: "This route needs to be accessed with a subdomain" at (...) line 86 [] [] 

我缺少什么? 谢谢您的帮助

最佳答案

在 kernel.request 事件期间不会捕获异常。您可以改为返回响应:

class SubdomainSubscriber implements EventSubscriberInterface
{

    public static function getSubscribedEvents()
    {
        return [
            'kernel.request' => 'onRequest',
        ];
    }

    public function onRequest(GetResponseEvent $event)
    {
        if (!$subdomainValid) {
            $event->setResponse(new Response('Invalid subdomain!', 403));
        }
    }
}

尽管此示例是一个 EventSubscriber,但它也可以与监听器一起使用。

关于symfony2 从 onKernelRequest 抛出异常返回空白页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16960265/

相关文章:

php - Doctrine 映射问题 - 对 null 调用成员函数 setValue()

php - Symfony2 Windows/Linux 的合适开发环境

c# - 当数据库中的字段不允许为空时如何处理异常

java - 使用 GET 调用 Java Servlet 但使用 POST 处理请求 - 通过 doGet 调用 doPost 在技术上仍然是 GET 操作?

unit-testing - 如何在 Symfony 包中设置特定于环境的参数?

spring - 如何在 Spring Security 中获取 AccessDeniedException 的 'reason'/原因?

c# - 将 foreach 循环嵌套到 lambda 表达式

javascript - react-native - 当获取结束时调用另一个函数

amazon-web-services - 不是你,是我们 我们现在无法完成您的请求。请稍后再试 - awsapps 登录

php - 具有多个选项的 Twig json_encode