Laravel Service Provider Development Setup problems encountered during delay loading

Source: Internet
Author: User
This article mainly introduces to you about the Laravel Service provider development set delay load encountered problems, the text through the sample code introduced in very detailed, to everyone's study or work has a certain reference learning value, the need for friends below with the small series to see together. Hope to help everyone.

Objective

This article mainly introduces some problems encountered in Laravel Service provider setting delay loading, all this article is due to the actual project needs, recently in the development of Laravel-database-logger package, found that the settings ServiceProvider The Defer property is set to True causes the middleware registered in the Register method to be invalid.

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, the Use Databaselogger middleware will error, prompt Databaselogger class not found. $this->app[\illuminate\routing\router::class]->middleware (' Databaselogger ', middleware::class); } Public  function provides () {return [Dblogger::class];}}

When the problem arises, it is suspected that the defer property is set to True, immediately modify the source code to protected $defer = true; Code is commented out, the result is still a hint Databaselogger class not found. , stating that Laravel did not register this Serviceprovder

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 serviceprovider in the normal registered Appserviceprovider

Public Function Register () {//$this->app->register (\ibrand\databaselogger\serviceprovider::class);}

After registering the result everything is OK.

2. Research source

Providers registration is not valid in config/app.php, but registration is valid in other serviceprovider, which indicates other issues.

Find the Registerconfiguredproviders method by researching illuminate\foundation\application source code:

Laravel is to read the providers content in config/app.php in this method and load it into providerrepository.

(New Providerrepository ($this, New Filesystem, $this->getcachedservicespath ()))     ->load ($providers->collapse ()->toarray ());

Focus on the $this->getcachedservicespath (), through the source found Laravel is based on bootstrap/cache/services.php file to decide how to register serviceprovider.

At this point I think of why the previous comment//protected $defer = true; The reason why the code is still invalid.

So in order to let the//protected after the comment $defer = true; Code valid requires execution

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 value, you need to execute PHP artisan clear-compiled and PHP artisan optimize to update the serviceprovider cache.

3. Why is registration valid in Appserviceprovider?

It is easy to be willing, because Appserviceprovider does not delay loading, so it is not deferred to register the new serviceprovider in the Appserviceprovider execution of the Register method.

Summarize

Use lazy loading serviceprovider sparingly

After changing the Defer property value, you need to execute PHP artisan clear-compiled and PHP artisan optimize to update the serviceprovider cache.

It is strictly forbidden to register middleware and route in the delayed loading serviceprovider.

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.