2

所以我正在检查 Laravel 的代码

我在看存储门面。我认为这就是它的加载方式。如果我错了,请纠正我。

  • 当我们访问

    Storage::get('someFile.txt');

  • 正在通过配置中的别名访问存储我正确吗?

    'Storage' => Illuminate\Support\Facades\Storage::class

  • 然后它将访问我相信的这个功能

protected static function getFacadeAccessor(){

  return 'filesystem';

}

  • 然后我认为返回文件系统正在访问存储在我相信的服务容器上的文件系统?这是在附加到容器的 FilesystemServiceProvider 中设置的。

protected function registerManager(){

  $this->app->singleton('filesystem', function () {
    return new FilesystemManager($this->app);
  });
}

所以总体而言,Facade 是在引用服务容器上的文件系统,对吗?

4

1 回答 1

1

是的,这是真的:laravel 中的所有 Facades 都是从服务容器中解析对象并在它们上调用方法的唯一便捷方法

所以,首先你在服务容器上注册一个绑定,一旦你完成了,而不是做

$fs = App::make('filesystem');
$file = $fs->get('someFile.txt');

你可以简单地做:

$file = Storage::get('someFile.txt');
于 2016-05-15T09:32:26.683 回答