Specifically what I am doing is in my AppServiceProvider->boot() method I am creating a singleton class like below:
class AppServiceProvider extends ServiceProvi
You can use the boot method for anything. From laravel docs:
This method is called after all other service providers have been registered, meaning you have access to all other services that have been registered by the framework
The key is that boot method run when all services are registered, so, you can inject services in the boot method definition.
public function boot(SomeService $someService, OtherService $otherService)
{
$someService->doSomething();
$otherService->doSomething();
}
In my opinion, you must to use this method to run code required by your app in all contexts: user logged in, user logged out, post, get, put, etc., etc.
IMHO you can try to add a terminating callback to your app instance, for example in the AppServiceProvider
, i.e.:
public function boot()
{
$this->app->terminating(function () {
// your terminating code here
});
}