Laravel Service Provider Problems when you set up lazy loading when developing a solution

Source: Internet
Author: User
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

    1. Use lazy loading with cautionServiceProvider

    2. After you change defer the property values, you need to perform php artisan clear-compiled and php artisan optimize update the serviceprovider cache.

    3. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.