每当我们调用Facade方法时,它都涉及Facade设计模式,并且它通过使用Facade调用一些隐藏类。例如文件,如果我们调用
File::get(public_path().'test.txt');这将在类中调用该方法。
Illuminate\Filesystem\Filesystem在这个类中,我们将使用get($path)方法。
现在我的问题是Facade抽象类如何与文件和文件系统相关,以及Laravel告诉他们调用get in Filesystem的位置。有没有我丢失的某种登记簿??我想找到完整的链接。
发布于 2019-02-09 18:41:33
如果您进入您的config/app.php,您将注意到有一个名为aliases的数组,它如下所示
'aliases' => [
//
//
//
//
'File' => Illuminate\Support\Facades\File::class,
]; 因此,只要您调用File,服务容器就会尝试解析Illuminate\Support\Facades\File::class的一个实例,这只是一个外观。
如果您查看Illuminate\Support\Facades\File::class,您将看到它只包含一个方法:
class File extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'files';
}
}正如您所看到的,它扩展了Facade类,每当解析一个Facade时,Laravel将试图在服务容器中找到一个与getFacadeAccessor()返回的任何内容相等的键。
如果您检查Illuminate\Filesystem\FilesystemServiceProvider的源,您将看到以下内容:
$this->app->singleton('files', function () {
return new Filesystem;
});如您所见,键files被限制在FileSystem实现上。这就是Laravel知道如何解析File外观的方式。
https://stackoverflow.com/questions/54607974
复制相似问题