Detailed description of laravel container delay loading and auth extension

Source: Internet
Author: User
This article mainly introduces laravel container delay loading and auth extension Details. if you need a friend, refer to yesterday's manual tutorial to write an Auth extension. according to the principle of package independence, I do not want to write the Auth: extend () method in start. in php, there is no doubt that I chose to register the extension driver in the register () method of the service provider. However, this is counterproductive ......

Problems found

When I write this in 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 the reason

At that time, I was wondering why I suspected Auth was not registered? Check and find that the domain name is registered, because it can be used in the routing; php artisan clear-compiled is useless; I was puzzled, and even suspected that I accidentally modified the core class, the laravel package is downloaded again, and the problem persists.

After one night of tossing, I finally got my eye on the $ defer attribute of AuthServiceProvider.

According to the manual and comments, we know that the $ defer attribute is used to delay loading the service provider. to put it bluntly, the register () method can be implemented with the provides () method. For 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 will be loaded in sequence. if the service provider protected $ defer = true, its provides () will be called () METHOD. the returned array contains the name of the service that requires delayed loading. in this way, when Auth: METHOD () is called in a route, controller, or elsewhere, to call the register () method of the provider.

Identify the Crux

The problem arises. since it is passive delayed loading, that is to say, the Auth class should be automatically instantiated when I call the Auth class method. Why did I prompt that the method does not exist when I call the method in LoauthServiceProvider, but it does in routing.

I guess it is because of a priority issue. when the framework registers LoauthServiceProvider: register (), Auth is not marked as delayed loading, which leads to a sequential problem, any real-time loading service provider cannot call the delayed loading service in the register method.

After research, we found evidence in the core code: Illuminate \ Foundation \ ProviderRepository

The code is as follows:


Public function load (Application $ app, array $ providers)
{
//... Omitted
// We will go ahead and register all of the eagerly loaded providers with
// Application so their services can be registered with the application
// A provided service. Then we will set the deferred service list on it.
Foreach ($ manifest ['ager'] as $ provider)
{
$ App-> register ($ this-> createProvider ($ app, $ provider ));
}
// Delayed loading mark after the service is loaded in real time
$ App-> setDeferredServices ($ manifest ['referred']);
}

Solution

Although the problem is found, it does not mean that the problem is solved. modifying the core code is not a wise choice, so we can only find a solution in our own package. one 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 can manually call its register method to register it.

The above is all the content of this article. I hope you will like it.

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.