【问题标题】:Laravel 5: Send a response first, then process request in controllerLaravel 5:先发送响应,然后在控制器中处理请求
【发布时间】:2015-06-11 02:27:18
【问题描述】:

我正在使用 Classic Paypal API,但在处理请求数据之前遇到了响应问题。

public function store() {
    // Send an empty HTTP 200 OK response to acknowledge receipt of the notification
    response("", 200);
    // Build the required acknowledgement message out of the notification just received
    // Once it hits this point, nothing is sent to the client.
}

我知道为了让客户端接收到 HTTP 200 响应,我需要在它前面添加 return 关键字。但是,如果我立即返回响应,则不会发生请求的处理。我研究了中间件之前和之后,但不幸的是它们不是异步的。有什么方法可以在 Laravel 5 中完成发送然后处理?

【问题讨论】:

标签: php laravel laravel-5


【解决方案1】:

我找到了解决这个问题的方法:

try {
    return response("", 200);
} finally {
    // Controller logic here
}

【讨论】:

  • 我最后一次测试这个是在 Laravel 5.2 上,所以它应该适用于 5.2,但是如果你还在使用 5.2,我强烈建议迁移到 5.5,因为 5.2 是 EOL。
  • 我们想向上移动,即使有这个解决方案 nginx 网关超时
  • 我最后一次测试这个是在 Apache 2.4 上而不是在 nginx 上。你想在这里做什么?
  • 我试图跳过队列实现一段时间,并在控制器中完成了一些繁重的处理,因此它可以回答然后继续处理
  • 您可能可以使用 Laravel 作业队列来处理控制器之外的繁重处理。您可以做的是让控制器将作业提交到队列中,并在将作业添加到队列中时立即返回成功响应。然后在您的队列侦听器中执行处理。这样你也不会受到 PHP 执行时间的限制。
猜你喜欢
  • 2015-09-21
  • 2020-08-06
  • 2020-07-27
  • 1970-01-01
  • 1970-01-01
  • 2011-07-24
  • 1970-01-01
  • 2023-01-18
  • 1970-01-01
相关资源
最近更新 更多