What event is fired when Laravel app is being shutdown?

前端 未结 2 1444
失恋的感觉
失恋的感觉 2021-01-21 20:15

Specifically what I am doing is in my AppServiceProvider->boot() method I am creating a singleton class like below:

class AppServiceProvider extends ServiceProvi         


        
相关标签:
2条回答
  • 2021-01-21 20:58

    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.

    0 讨论(0)
  • 2021-01-21 21:11

    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
        });
    }
    
    0 讨论(0)
提交回复
热议问题