Registering a Finish Callback

Problem

You want to have code execute after the request is sent to the user, but before the application terminates.

Solution

Use the App::finish() method to register a finish callback.

App::finish(function($request, $response)
{
    // Use request and/or response to do logging or some after
    // main processing stuff
});

NOTE: Although $request and $response are provided to the callback, modification of either of these will have no affect in the application.

Discussion

Where do you register the callbacks?

You can put the App::finish() call in a service provider or even app/start/global.php. Since any callbacks are not called until late in the process the question "Where to put them?" doesn't matter too much.

Look at Understanding the Request Lifecycle. The second to last step in the section labeled The Running Steps shows when this is called.

comments powered by Disqus