Skip to content

Instantly share code, notes, and snippets.

@wallacemaxters
Created September 30, 2017 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wallacemaxters/3310e63610797776a1bbd86ab91054ac to your computer and use it in GitHub Desktop.
Save wallacemaxters/3310e63610797776a1bbd86ab91054ac to your computer and use it in GitHub Desktop.
Execute function in Laravel after response sent to client
<?php
Route::get('/', function () {
$response = Response::json([
'process' => true
]);
return Response::withShutdownTask($response, function () {
file_put_contents(public_path('__task__.txt'), 'teste');
});
});
Response::macro('withShutdownTask', function ($response, callable $callback) {
$response->header('Connection', 'Close');
$response->header('Content-Encoding', 'none');
$response->header('Content-Length', mb_strlen($response->getContent()));
register_shutdown_function(static function () use ($callback) {
ignore_user_abort(true);
flush();
@ob_end_flush();
sleep(3);
$callback();
});
ob_start();
return $response;
});
@adisonmasih
Copy link

does it work in 2022? 🤣🤣

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment