Recently in the development of the Laravel-database-logger package, the discovery set serviceprovider
deferproperty is set to
trueWill cause the
registerMethod that is registered in the
middlewareInvalid. This article is mainly to share with you laravel Service Provider development When set delay load problems encountered in the way, I hope to help everyone.
Class ServiceProvider extends \illuminate\support\serviceprovider{ protected $defer = true; Public Function Register () { $this->mergeconfigfrom ( __dir__. '/.. /config/config.php ', ' Ibrand.dblogger ' ); $this->app->singleton (Dblogger::class, function ($app) { return new Dblogger (); }); When the $defer is set to true, referencing Databaselogger middleware in the route will give an error, prompting Databaselogger class not found. $this->app[\illuminate\routing\router::class]->middleware (' Databaselogger ', middleware::class); } Public function provides () { return [dblogger::class]; }}
When the problem arises in the doubt because the setting of the defer property set to true cause, immediately modify protected $defer = true; the source code to comment out, the result is still a hint databaselogger class not found. , stating that Laravel did not register thisServiceProvder
The next step is to figure out how to solve this problem, and try the following method:
1. Verify that there is a problem with your own code
Register your own in the normal registration AppServiceProviderServiceProvider
Public Function Register () { // $this->app->register (\ibrand\databaselogger\serviceprovider:: Class); }
After registering the result everything is OK.
2. Research source
config/app.php providers Registration is not valid in, but ServiceProvider registration is valid in other, which indicates other issues.
Find the method by researching the Illuminate\Foundation\Application source code registerConfiguredProviders :
Laravel is in this method to read config/app.php the providers contents and load into the ProviderRepository .
(New Providerrepository ($this, New Filesystem, $this->getcachedservicespath ())) ->load ($providers->collapse ()->toarray ());
Focus on $this->getCachedServicesPath() , through the source Discovery Laravel is based on the bootstrap/cache/services.php document to decide how to register ServiceProvider .
At this point I thought about why //protected $defer = true; the code was still invalid after commenting it.
So in order for the annotated //protected $defer = true; code to be effective, it needs to be executed
PHP artisan clear-compiled php Artisan optimize
After that, the problem was solved, and the principle of serviceprovider was further understood.
so remember: If you are prepared to use deferred loading
ServiceProvider , do not register middleware, route and other series of operations. Also, after changing the
defer property values, you need to perform
php artisan clear-compiled and
php artisan optimize update the serviceprovider cache.
3. Why is registration valid in Appserviceprovider?
Willing to be simple, because AppServiceProvider there is no delay in loading, so in the execution of the AppServiceProvider register method to register the new ServiceProvider is not delayed loading.
Summarize
Use lazy loading with cautionServiceProvider
After you change defer the property values, you need to perform php artisan clear-compiled and php artisan optimize update the serviceprovider cache.
Registration and the delay loading are strictly prohibited ServiceProvider middleware route .
Related recommendations:
Laravel5 How to create service provider and facade
Laravel Service Providers Issues
Laravel5 How to create service provider and facade