Laravel container delay loading and auth Extensions _php tutorial

Source: Internet
Author: User

Laravel container delay loading and auth extension


Yesterday follow the manual tutorial, write a auth extension, according to the principle of package independence, I do not want to auth::extend () This method written in start.php, no doubt, I chose to register the extension driver in the service Provider register () method. However, it backfired ...

Discover problems

When I write this in the Loauthserviceprovider:

The code is as follows:


Public Function Register ()
{
//
\auth::extend (' Loauth ', function ($app) {});
}

Error

The code is as follows:


Call to undefined method Illuminate\support\facades\auth::extend ()

Find out why

At that time, I was puzzled, to find the reason, suspicion is auth not registered? Check that the discovery is registered, because it can be used in the route; PHP artisan clear-compiled useless; baffled, even suspected that I accidentally modified the core class, but also downloaded a laravel package again, the problem remains.

Tossing all night, I finally locked my eyes in the Authserviceprovider $defer properties.

According to the manual and the note, we learned that the $defer property is used to delay loading the service provider, and that the straightforward point is to delay the execution of the Register () method, which only needs to be implemented with the provides () method. As an example:

The code is as follows:


Public Function provides ()
{
Return Array (' auth ');
}

This is the method in Authserviceprovider, when the framework is initialized, the service provider is loaded sequentially, and if this service provider protected $defer =true then it will call its provides () method, The returned array contains the name of the service that needs to be lazily loaded, so that when we call Auth::method () in the routing, controller, or elsewhere, the register () method of the provider is called.

Identify the crux

Then the problem comes, since it is passive lazy loading, that is, when I call the Auth class method should be automatically instantiated Auth class Ah, why I call in Loauthserviceprovider when the method does not exist, but in the route can be.

My guess is that because of the priority problem, maybe when the framework registers Loauthserviceprovider::register (), Auth has not been marked as deferred loading, which creates a succession problem, Any immediately loaded service provider cannot invoke a deferred-loaded service in the Register method.

After research, find the evidence in the core code successfully illuminate\foundation\providerrepository

The code is as follows:


Public function load (application $app, array $providers)
{
//... Omitted
We'll go ahead and register all of the eagerly loaded providers with the
Application so their services can is registered with the application as
A provided service. Then we'll set the deferred service list on it.
foreach ($manifest [' eager '] as $provider)
{
$app->register ($this->createprovider ($app, $provider));
}
Deferred load tag after instant load service
$app->setdeferredservices ($manifest [' deferred ']);
}

The way to solve

Although the problem is found, but does not mean that the problem is solved, modify the core code is not a wise choice, so only in our own package to find out, a solution is as follows:

The code is as follows:


Public Function Register ()
{
//
$authProvider = new \illuminate\auth\authserviceprovider ($this->app);
$authProvider->register ();
\auth::extend (' Loauth ', function ($app) {});
}

Since Auth is not yet registered, we manually call its register method to sign it.

The above is the whole content of this article, I hope you can enjoy.

http://www.bkjia.com/PHPjc/962934.html www.bkjia.com true http://www.bkjia.com/PHPjc/962934.html techarticle laravel container delay loading and auth extension details yesterday follow the manual tutorial, write a auth extension, according to the principle of package independence, I do not want to auth::extend () This method written in ...

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