Home  >  Article  >  Backend Development  >  Detailed explanation of fastcgi_finish_request() function in Laravel

Detailed explanation of fastcgi_finish_request() function in Laravel

零下一度
零下一度Original
2017-06-23 13:51:182621browse

##fastcgi_finish_request() is provided by php-fpm to end the connection response data early and continue to perform tasks on the backend Function, in order to perform long-term tasks or improve response speed, we can use this function for simple implementation. Of course, a better way is to use a queue.

In Laravel, sending a response will call

fastcgi_finish_request() (if it exists), and then the terminate() method will be called, which will execute the registration in sequence. terminate() method in middleware.

Using Terminable Middleware in the document requires registering global middleware, but in practice, we may only need to respond in advance to some interfaces and then process subsequent logic (database, logs...). But as you can see from the source code, Laravel's

routeMiddleware actually supports Terminable Middleware, so we can define a FinishRequest middleware as follows to register for use.

<?phpnamespace App\Http\Middleware;use Closure;class FinishRequest
{protected static $next;/**     * Handle an incoming request.     *     * @param  \Illuminate\Http\Request $request     * @param  \Closure $next     * @return mixed     */public function handle($request, Closure $next){self::$next = $next;return response([&#39;code&#39; => 0, &#39;data&#39; => [], &#39;msg&#39; => &#39;&#39;]);}/**     * @param \Illuminate\Http\Request $request     * @param $response     */public function terminate($request, $response){call_user_func(self::$next, $request);}
}

The above is the detailed content of Detailed explanation of fastcgi_finish_request() function in Laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn