is introduced in this paper. Some problems about Laravel Service Provider set the delay when loading, all of this article, is due to the actual needs of the project, recently in the development of laravel-database-logger package, that set the ServiceProvider defer property is set to true, registered in the register method will lead to invalid middleware.

 class ServiceProvider extends IlluminateSupportServiceProvider $defer {protected = true; public function (register) {$this-> mergeConfigFrom (__DIR__.'/../config/config.php','ibrand.dblogger'); $this-> app-> singleton (DbLogger:: class, function ($app) {return new (DbLogger);}); / / when $defer is set to true, databaselogger Middleware the error quoted in routing, suggesting that databaselogger class not found. $this-> app[IlluminateRouting; Router:: class]-> middleware ('databaselogger', Middleware:, class);} public function provides (return) {[DbLogger:: class];}


when problems arise because of suspected set the defer attribute set to true result, immediately to modify the source code protected $defer = true; code comments off, the result is still class not found. suggested that databaselogger, Laravel and the ServiceProvder

is not registered next to how to solve this problem, try the following methods:


 public to register their own function register in the normal registration in AppServiceProvider () {/ / $this-> app-> register (IbrandDatabaseLoggerServiceProvider:: class);}

after the registration results all normal.

2. research source

config/app.php registration invalid in config/app.php, but registration in other ServiceProvider is valid, indicating other problems.

finds the registerConfiguredProviders method by studying the source of IlluminateFoundationApplication:

Laravel reads providers contents in config/app.php and load to ProviderRepository in this way.

 (New ProviderRepository ($this new, Filesystem, $this->); getCachedServicesPath (->)); load ($providers-> collapse) (->); toArray (


$this-> (getCachedServicesPath) in the key; , through the source that Laravel is based on the bootstrap/cache/services.php file to determine how to register ServiceProvider.

at this time thought about why //protected $defer = true was annotated before; the reason why the code was still invalid was still after the code.

so to make notes after //protected $defer = true; code after the

 PHP needs artisan clear-compiled PHP artisan optimize

can solve the problem, but also a more in-depth understanding of the principle of ServiceProvider.

so remember: if you are ready to use a delayed loading of ServiceProvider, no registration of middleware, route, and other series of operations. Meanwhile, after changing the defer attribute value, we need to execute php artisan clear-compiled and php artisan optimize to update ServiceProvider cache. Why is

3. valid for registration in AppServiceProvider?

is willing to be very simple, because AppServiceProvider does not delay loading, so in executing AppServiceProvider, register method to register new ServiceProvider will not be delayed loading.

summary careful use of the delay load ServiceProvider

to change the value of the defer property, artisan and clear-compiled php to implement php artisan optimize to a new ServiceProvider cache.

is strictly forbidden to register middleware and route in delayed loaded ServiceProvider.

, OK, the above is the whole content of this article. I hope that the content of this article will have some reference value for everyone's study or work. If there's any doubt, you can leave a message to exchange, thank you for supporting the script home.

This concludes the body part