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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With