gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 04:54:27 35 4
gpt4 key购买 nike

我有一个与 kernel.request 事件 Hook 的事件监听器。在 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/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com