laravel容器延迟加载以及auth扩展详解_PHP
昨天按照手册教程,动手写一个Auth扩展,按照包独立性的原则,我不希望将Auth::extend()这种方法写在 start.php 中,毫无疑问,我选择了在服务提供器register()方法中注册扩展驱动。然而,事与愿违……
发现问题
当我在 LoauthServiceProvider 中这样写的时候:
代码如下:
public function register()
{
//
\Auth::extend('loauth',function($app){});
}
报错
代码如下:
Call to undefined method Illuminate\Support\Facades\Auth::extend()
寻找原因
当时就纳闷了,找原因,怀疑是Auth没注册?检查发现注册了,因为在路由中可以使用;php artisan clear-compiled 没用;百思不得其解,甚至怀疑是我不小心修改了核心类,还重新下载了一次laravel包,问题依旧。
折腾了一晚上,最终我把目光锁定在 AuthServiceProvider 的 $defer 属性。
根据手册以及注释,我们得知 $defer 属性是用来延迟加载该服务提供器,说直白点就是延迟执行 register() 方法,只需要配合provides()方法即可实现。举个例子:
代码如下:
public function provides()
{
return array('auth');
}
这个是 AuthServiceProvider 里的方法,当框架初始化的时候,会依次加载服务提供器,如果发现这个服务提供器protected $defer=true 那么就会调用它的 provides() 方法,其返回的数组包含需要延迟加载的服务名称,这样当我们在路由、控制器或者其他地方调用 Auth::METHOD() 的时候,才会去调用提供器的 register() 方法。
确定症结
那么问题来了,既然是被动延迟加载,也就是说当我调用Auth类方法时应该会自动实例化Auth类啊,为什么我在LoauthServiceProvider中调用的时候却提示方法不存在,但是在路由中却可以呢。
我猜测是因为优先级的问题,可能在框架注册 LoauthServiceProvider::register() 的时候,Auth 还没有标记为延迟加载,这就造成了一个先后问题,任何即时加载的服务提供器都无法在register方法中调用延迟加载的服务。
经过研究,顺利在核心代码中找到证据 Illuminate\Foundation\ProviderRepository
代码如下:
public function load(Application $app, array $providers)
{
//...省略
// 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));
}
//延迟加载标记在即时加载服务之后
$app->setDeferredServices($manifest['deferred']);
}
解决之道
虽然发现了问题所在,但并不代表问题就解决了,修改核心代码不是个明智的选择,所以只能在我们自己的包里想办法咯,一个解决方案如下:
代码如下:
public function register()
{
//
$authProvider = new \Illuminate\Auth\AuthServiceProvider($this->app);
$authProvider->register();
\Auth::extend('loauth',function($app){});
}
既然auth还未注册,那么我们手动调用它的register方法帮它注册。
以上就是本文的全部内容了,希望大家能够喜欢。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The latest versions of Laravel 9 and CodeIgniter 4 provide updated features and improvements. Laravel9 adopts MVC architecture and provides functions such as database migration, authentication and template engine. CodeIgniter4 uses HMVC architecture to provide routing, ORM and caching. In terms of performance, Laravel9's service provider-based design pattern and CodeIgniter4's lightweight framework give it excellent performance. In practical applications, Laravel9 is suitable for complex projects that require flexibility and powerful functions, while CodeIgniter4 is suitable for rapid development and small applications.

For beginners, CodeIgniter has a gentler learning curve and fewer features, but covers basic needs. Laravel offers a wider feature set but has a slightly steeper learning curve. In terms of performance, both Laravel and CodeIgniter perform well. Laravel has more extensive documentation and active community support, while CodeIgniter is simpler, lightweight, and has strong security features. In the practical case of building a blogging application, Laravel's EloquentORM simplifies data manipulation, while CodeIgniter requires more manual configuration.

When choosing a framework for large projects, Laravel and CodeIgniter each have their own advantages. Laravel is designed for enterprise-level applications, offering modular design, dependency injection, and a powerful feature set. CodeIgniter is a lightweight framework more suitable for small to medium-sized projects, emphasizing speed and ease of use. For large projects with complex requirements and a large number of users, Laravel's power and scalability are more suitable. For simple projects or situations with limited resources, CodeIgniter's lightweight and rapid development capabilities are more ideal.

Laravel - Artisan Commands - Laravel 5.7 comes with new way of treating and testing new commands. It includes a new feature of testing artisan commands and the demonstration is mentioned below ?

For small projects, Laravel is suitable for larger projects that require strong functionality and security. CodeIgniter is suitable for very small projects that require lightweight and ease of use.

Comparing Laravel's Blade and CodeIgniter's Twig template engine, choose based on project needs and personal preferences: Blade is based on MVC syntax, which encourages good code organization and template inheritance. Twig is a third-party library that provides flexible syntax, powerful filters, extended support, and security sandboxing.

Laravel - Artisan Console - Laravel framework provides three primary tools for interaction through command-line namely: Artisan, Ticker and REPL. This chapter explains about Artisan in detail.

Laravel - Pagination Customizations - Laravel includes a feature of pagination which helps a user or a developer to include a pagination feature. Laravel paginator is integrated with the query builder and Eloquent ORM. The paginate method automatical
