In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "laravel container delay loading and auth expansion method", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "laravel container delay loading and auth expansion method" article.
Find a problem
When I write this in LoauthServiceProvider:
The copy code is as follows:
Public function register ()
{
/ /
\ Auth::extend ('loauth',function ($app) {})
}
Error
The copy code is as follows:
Call to undefined method Illuminate\ Support\ Facades\ Auth::extend ()
Look for the cause
At that time, I wondered, looking for the reason, suspected that Auth was not registered? I checked and found that I was registered because it could be used in routing; php artisan clear-compiled was useless; I was so puzzled that I even suspected that I had accidentally modified the core class and downloaded the laravel package again, and the problem remained.
After messing around all night, I finally set my sights on the $defer attribute of AuthServiceProvider.
According to the manual and comments, we know that the $defer attribute is used to delay the loading of the service provider, and to put it bluntly is to delay the execution of the register () method, which can be achieved with the provides () method. For example:
The copy code is as follows:
Public function provides ()
{
Return array ('auth')
}
This is the method in AuthServiceProvider. When the framework initializes, the service provider loads in turn. If the service provider protected $defer=true is found, then its provides () method is called, and the returned array contains the service names that need to be delayed loading, so that when we call Auth::METHOD () on the route, controller, or elsewhere, we will call the provider's register () method.
Determine the crux of the problem
So the problem is, since it is passive deferred loading, that is to say, when I call the Auth class method, I should instantiate the Auth class automatically. Why do I prompt that the method does not exist when I call it in LoauthServiceProvider, but it is OK in the route?
I guess it's because of priority, maybe when the framework registers LoauthServiceProvider::register (), Auth is not marked as lazy loading, which creates a priority problem. No service provider that loads instantly can call a deferred loading service in the register method.
After research, the evidence Illuminate\ Foundation\ ProviderRepository was successfully found in the core code.
The copy code is as follows:
Public function load (Application $app, array $providers)
{
/ /... Omit
/ / We will go ahead and register all of the eagerly loaded providers with the
/ / application so their services can be registered with the application as
/ / a provided service. Then we will set the deferred service list on it.
Foreach ($manifest ['eager'] as $provider)
{
$app- > register ($this- > createProvider ($app, $provider))
}
/ / delayed loading tag after the service is loaded immediately
$app- > setDeferredServices ($manifest ['deferred'])
}
The solution
Although we have found the problem, it does not mean that the problem has been solved. It is not a wise choice to modify the core code, so we can only figure it out in our own package. One solution is as follows:
The copy 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 registered yet, we manually call its register method to help it register.
The above is the content of this article on "delayed loading of laravel containers and auth expansion". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.