Laravel container delay loading and auth extension details, laravelauth. Laravel container delay loading and auth extension Details. laravelauth wrote an Auth extension according to the manual tutorial yesterday. according to the packet independence principle, I do not want to add Auth: extend () laravelauth
I wrote an Auth extension according to the manual tutorial yesterday. according to the packet independence principle, 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.
Parse wrote an Auth extension according to the manual tutorial yesterday. according to the packet independence principle, I do not want to write Auth: extend...