“Facades 为应用的 服务容器 提供了一个「静态」 接口。Laravel 自带了很多 Facades,可以访问绝大部分功能。Laravel Facades 实际是服务容器中底层类的 「静态代理」 ,相对于传统静态方法,在使用时能够提供更加灵活、更加易于测试、更加优雅的语法。”
如何使用Facades?
我们使用一个 Laravel 中的例子,来说明一下 Facade 是如何简化调用的。我们需要调用设置数据缓存的方法,使用 Facade 的语法如下:
#所有的 Laravel Facades 都定义在 Illuminate\Support\Facades 命名空间下。
#所以,我们可以轻松的使用 Facade :
use Illuminate\Support\Facades\Cache;
Route::get('/cache', function() {
// 事先保证执行下面的put方法,将缓存存入
// Cache::put('key', 'HelloKang', 10);
// 获取缓存项key的内容
return Cache::get('key');
}
如果不使用 Facade 来调用,那么调用的语法如下:
Route::get('/cache', function() {
// 获取缓存项key的内容
return app('cache')->get('key');
}
语法过程就是先从服务容器中解析出来缓存对象,再利用缓存对象将缓存项提取。
对比这两种使用方式,第一种显而易见,更加直观,简单一些。这就是 Facade 的主要目的。
Facade的更通用的用法是省略前缀Illuminate\Support\Facades,直接use Cache进来,或者直接在代码中以\Cache::get()的方式调用,因为在配置目录config目录下的app.php文件中 aliases 已经为我们导入了类别名。
'aliases' => [
'App' => Illuminate\Support\Facades\App::class,
'Artisan' => Illuminate\Support\Facades\Artisan::class,
'Auth' => Illuminate\Support\Facades\Auth::class,
'Blade' => Illuminate\Support\Facades\Blade::class,
'Broadcast' => Illuminate\Support\Facades\Broadcast::class,
'Bus' => Illuminate\Support\Facades\Bus::class,
'Cache' => Illuminate\Support\Facades\Cache::class,
'Config' => Illuminate\Support\Facades\Config::class,
'Cookie' => Illuminate\Support\Facades\Cookie::class,
'Crypt' => Illuminate\Support\Facades\Crypt::class,
'DB' => Illuminate\Support\Facades\DB::class,
'Eloquent' => Illuminate\Database\Eloquent\Model::class,
'Event' => Illuminate\Support\Facades\Event::class,
'File' => Illuminate\Support\Facades\File::class,
'Gate' => Illuminate\Support\Facades\Gate::class,
'Hash' => Illuminate\Support\Facades\Hash::class,
'Lang' => Illuminate\Support\Facades\Lang::class,
'Log' => Illuminate\Support\Facades\Log::class,
'Mail' => Illuminate\Support\Facades\Mail::class,
'Notification' => Illuminate\Support\Facades\Notification::class,
'Password' => Illuminate\Support\Facades\Password::class,
'Queue' => Illuminate\Support\Facades\Queue::class,
'Redirect' => Illuminate\Support\Facades\Redirect::class,
'Redis' => Illuminate\Support\Facades\Redis::class,
'Request' => Illuminate\Support\Facades\Request::class,
'Response' => Illuminate\Support\Facades\Response::class,
'Route' => Illuminate\Support\Facades\Route::class,
'Schema' => Illuminate\Support\Facades\Schema::class,
'Session' => Illuminate\Support\Facades\Session::class,
'Storage' => Illuminate\Support\Facades\Storage::class,
'URL' => Illuminate\Support\Facades\URL::class,
'Validator' => Illuminate\Support\Facades\Validator::class,
'View' => Illuminate\Support\Facades\View::class,
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
'Input' => Illuminate\Support\Facades\Input::class,
'EasyWeChat' => Overtrue\LaravelWeChat\Facade::class,
'Image' => Intervention\Image\Facades\Image::class,
'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class,
],
Facade,就是访问服务方法的快捷语法,不同我们从服务容器中手动解析,直接调用封装好的 Facade 即可完成任务。
看到这里就明白了,缓存Facade,Cache类的实现只是告诉服务容器,我需要的服务名字叫“cache”,你把之前绑定的类实例化后返回给我用。至于“cache”服务是何时绑定到容器中的,这就涉及框架的启动过程以及容器类的内部实现了。