Created
September 30, 2017 17:30
-
-
Save wallacemaxters/3310e63610797776a1bbd86ab91054ac to your computer and use it in GitHub Desktop.
Execute function in Laravel after response sent to client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👀