Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel terminable middleware gets called before returning response

I use postman to query my laravel API and the terminate method in my middleware gets called before postman receives the result:

Route

 Route::any('/lookup', 'LookupController@lookup')->name('lookup')->middleware(\App\Http\Middleware\LogRequest::class);

Middleware

namespace App\Http\Middleware;

use Closure;


class LogRequest
{
    public function handle($request, Closure $next)
    {
        return $next($request);
    }

    public function terminate($request, $response)
    {
        sleep(10);
    }
}

The response arrives 10 seconds earlier when I remove the sleep(10); So I am sure that the server waits for terminate to finish before actually returning the response.

like image 709
Chris Avatar asked Sep 18 '25 18:09

Chris


1 Answers

You need to have FastCGI for this to work, the fastcgi_finish_request() function must be available. I have the same problem with Laravel Sail

Running terminable middleware requires PHP’s fastcgi_finish_request() function, which closes the connection to the client while keeping the PHP worker available to finish the request (i.e., execute the code in your terminate() method).

like image 128
Ratto Avatar answered Sep 21 '25 08:09

Ratto