gpt4 book ai didi

php - Symfony2 : force stop code execution after sendiing Response headers

转载 作者:行者123 更新时间:2023-12-04 20:38:39 25 4
gpt4 key购买 nike

发送响应头后停止代码执行但不使用 exit() 的正确方法是什么?

我知道脚本应该返回一个响应,但我如何强制它从 Controller 外部返回,例如从服务返回?

假设我的服务方法确实以这种方式返回响应:

return RedirectResponse($url)->send();

但是在另一个地方它可以返回其他东西。所以我能做的就是检查它实际返回的内容:

$result = $myService->doSomething();
if ($result instanceof RedirectResponse) {
return $result;
}

我想要实现的是避免在我使用我的服务的每个地方检查结果类型,但我希望在 Profiler/logs 中看到返回的响应(如果我使用 exit() 我不能)。

有没有办法强制内核终止?


编辑:

实际上该服务在任何 Controller 之前的事件中使用,所以我想在任何 Controller 执行之前进行重定向。那么也许是一种省略 Controller 执行的方法?

最佳答案

A controller is a PHP callable you create that takes information from the HTTP request and creates and returns an HTTP response (as a Symfony Response object).

Controller 唯一关心的是处理请求并返回响应。让服务处理响应对象可能是一个糟糕的设计选择。

在任何情况下,您都可以终止/退出您的 Controller 并使用内核事件 Hook 请求/响应流并检查返回的响应。终止事件可能是正确的选择 http://symfony.com/doc/current/components/http_kernel/introduction.html

关于php - Symfony2 : force stop code execution after sendiing Response headers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31584341/

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