* * * * *
[TOC]
## 簡介
Facades(讀音:/f??s?d/ )為應用程序的?[服務容器](http://www.hmoore.net/tonyyu/laravel_5_6/786056)?中可用的類提供了一個「靜態」接口。Laravel 自帶了很多 Facades ,可以訪問絕大部分 Laravel 的功能。Laravel Facades 實際上是服務容器中底層類的「靜態代理」,它提供了簡潔而富有表現力的語法,甚至比傳統的靜態方法更具可測試性和擴展性。
所有的 Laravel Facades 都在?`Illuminate\Support\Facades`?命名空間中定義。所以,我們可以輕松地使用 Facade :
~~~
use Illuminate\Support\Facades\Cache;
Route::get('/cache', function () {
return Cache::get('key');
});
~~~
在 Laravel 的文檔中,很多示例代碼都會使用 Facades 來演示框架的各種功能。
## 何時使用 Facades
Facades 有很多好處, 它為我們使用 Laravel 的功能提供了簡單、易記的語法,而無需記住必須手動注入或配置的長長的類名。此外,由于他們對 PHP 動態方法的獨特用法,使得測試起來非常容易。
然而,在使用 Facades 時,有些地方需要特別注意。使用 Facades 最主要的風險就是會引起類作用范圍的膨脹。因為 Facades 使用起來非常簡單且不需要注入,就會使得我們不經意間在單個類中使用許多 Facades,從而導致類變的越來越大。而使用依賴注入的時候,使用的類越多,構造方法就會越長,在視覺上就會引起注意,提醒你這個類有些龐大了。 因此在使用 Facades的時候,要特別注意控制類的大小,讓類的作用范圍保持短小。
> {tip} 在開發與 Laravel 進行交互的第三方擴展包時, 建立最好選擇注入?[Laravel 契約](http://www.hmoore.net/tonyyu/laravel_5_6/786059)?,而不是使用 Facades的方法來使用類。因為擴展包是在 Laravel 本身之外構建,所以你無法使用 Laravel Facades 測試輔助函數。
### Facades Vs. 依賴注入
依賴注入的主要優點之一是切換注入類的實現的能力。 這在測試的時候很有用,因為你可以注入一個 mock 或者 stub ,并斷言在 stub 上調用的各種方法。
通常,真正的靜態方法是不可能被 mock 或者 stub 。但是,因為 Facades 使用動態方法來代理從服務容器解析的對象的方法調用,我們可以像測試注入的類實例一樣來測試 Facades。例如,像下面的路由:
~~~
use Illuminate\Support\Facades\Cache;
Route::get('/cache', function () {
return Cache::get('key');
});
~~~
我們可以用下面的測試代碼來驗證帶預期參數的?`Cache::get`?方法
~~~
use Illuminate\Support\Facades\Cache;
/**
* 一個基礎功能的測試用例。
*
* @return void
*/
public function testBasicExample()
{
Cache::shouldReceive('get')
->with('key')
->andReturn('value');
$this->visit('/cache')
->see('value');
}
~~~
### Facades Vs. 輔助函數
除了 Facades, Laravel 還包含各種 『輔助函數』來實現一些常用功能,比如生成視圖、觸發事件、任務調度或者發送 HTTP 響應。 許多輔助函數都有與之對應的 Facade 。 例如,下面這個 Facade 和輔助函數的作用是一樣的。
~~~
return View::make('profile');
return view('profile');
~~~
這里的 Facades 和輔助函數之間沒有實際的區別。當你使用輔助函數時,你可以使用對應的 Facade 進行測試。例如,下面的路由:
~~~
Route::get('/cache', function () {
return cache('key');
});
~~~
在底層實現,輔助函數?`cache`?實際是調用?`Cache`?這個 facade 的?`get`?方法。因此,盡管我們使用的是輔助函數,我們依然可以編寫以下測試來驗證該方法是否正常調用:
~~~
use Illuminate\Support\Facades\Cache;
/**
* 一個基礎功能的測試用例
*
* @return void
*/
public function testBasicExample()
{
Cache::shouldReceive('get')
->with('key')
->andReturn('value');
$this->visit('/cache')
->see('value');
}
~~~
## Facades 工作原理
在 Laravel 應用中, Facade 就是一個可以從容器訪問對象的類。其中核心的部件就是?`Facade`?類。 不管是 Laravel 自帶的 Facades , 還是自定義的 Facades ,都繼承自?`Illuminate\Support\Facades\Facade`?類。
`Facade`?基類使用了?`__callStatic()`?魔術方法,直到對象從容器中被解析出來后,才會進行調用。在下面的例子中,調用了 Laravel 的緩存系統。通過瀏覽這段代碼,可以假定在?`Cache`類中調用了靜態方法?`get`?:
~~~
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
{
/**
* 顯示給定用戶的信息。
*
* @param int $id
* @return Response
*/
public function showProfile($id)
{
$user = Cache::get('user:'.$id);
return view('profile', ['user' => $user]);
}
}
~~~
注意在上面這段代碼中,我們『導入』了?`Cache`?Facade.。這個 Facade作為訪問?`Illuminate\Contracts\Cache\Factory`?接口底層實現的代理,我們使用 Facade進行的任何調用都將傳遞給 Laravel 緩存服務的底層實例。
如果我們看一下?`Illuminate\Support\Facades\Cache`?這個類,你會發現類中根本沒有?`get`?這個靜態方法:
~~~
class Cache extends Facade
{
/**
* 獲取組件的注冊名稱。
*
* @return string
*/
protected static function getFacadeAccessor() { return 'cache'; }
}
~~~
`Cache`?Facade 繼承了`Facade`?類,并且定義了?`getFacadeAccessor()`?方法。這個方法的作用是返回服務容器綁定的名稱。當用戶調用?`Cache`?Facade 中的任何靜態方法時,Laravel 會從?[服務容器](http://www.hmoore.net/tonyyu/laravel_5_6/786056)?中解析?`cache`?綁定以及該對象運行所請求的方法(在這個例子中就是?`get`?方法)。
## 實時 Facades
使用實時 Facades,你可以將應用程序中的任何類視為 Facade。為了說明這是如何使用的,我們來看看另一種方法。例如,假設我們的?`Podcast`?模型有一個?`publish`?方法。然而,為了發布 Podcast,我們需要注入一個?`Publisher`實例:
~~~
<?php
namespace App;
use App\Contracts\Publisher;
use Illuminate\Database\Eloquent\Model;
class Podcast extends Model
{
/**
* 發布 Podcast。
*
* @param Publisher $publisher
* @return void
*/
public function publish(Publisher $publisher)
{
$this->update(['publishing' => now()]);
$publisher->publish($this);
}
}
~~~
將發布者的實現注入到該方法中,我們可以輕松地測試這種方法,因為我們可以模擬注入的發布者。但是,它要求我們每次調用?`publish`?方法時都要傳遞一個發布者實例。使用實時的 Facades,我們可以保持同樣的可測試性,而不需要顯式地通過?`Publisher`?實例。要生成實時外觀,請在導入類的名稱空間中加上?`Facades`:
~~~
<?php
namespace App;
use Facades\App\Contracts\Publisher;
use Illuminate\Database\Eloquent\Model;
class Podcast extends Model
{
/**
* 發布 Podcast.
*
* @return void
*/
public function publish()
{
$this->update(['publishing' => now()]);
Publisher::publish($this);
}
}
~~~
當使用實時 Facade 時,發布者實現將通過使用?`Facades`?前綴后出現的接口或類名的部分來解決服務容器的問題。在測試時,我們可以使用 Laravel 的內置 facade 測試輔助函數來模擬這種方法調用:
~~~
<?php
namespace Tests\Feature;
use App\Podcast;
use Tests\TestCase;
use Facades\App\Contracts\Publisher;
use Illuminate\Foundation\Testing\RefreshDatabase;
class PodcastTest extends TestCase
{
use RefreshDatabase;
/**
* 一個測試演示。
*
* @return void
*/
public function test_podcast_can_be_published()
{
$podcast = factory(Podcast::class)->create();
Publisher::shouldReceive('publish')->once()->with($podcast);
$podcast->publish();
}
}
~~~
## Facade 類參考
在下面你可以找到每個 Facade 類及其對應的底層類。這是一個查找給定 Facade 類 API 文檔的工具。[服務容器綁定](http://www.hmoore.net/tonyyu/laravel_5_6/786056)?的可用鍵值也包含在內。
| Facade | 類 | 服務容器綁定 |
| --- | --- | --- |
| App | [Illuminate\Foundation\Application](https://laravel.com/api/laravel/5.6/Illuminate/Foundation/Application.html) | `app` |
| Artisan | [Illuminate\Contracts\Console\Kernel](https://laravel.com/api/laravel/5.6/Illuminate/Contracts/Console/Kernel.html) | `artisan` |
| Auth | [Illuminate\Auth\AuthManager](https://laravel.com/api/laravel/5.6/Illuminate/Auth/AuthManager.html) | `auth` |
| Auth (Instance) | [Illuminate\Contracts\Auth\Guard](https://laravel.com/api/laravel/5.6/Illuminate/Contracts/Auth/Guard.html) | `auth.driver` |
| Blade | [Illuminate\View\Compilers\BladeCompiler](https://laravel.com/api/laravel/5.6/Illuminate/View/Compilers/BladeCompiler.html) | `blade.compiler` |
| Broadcast | [Illuminate\Contracts\Broadcasting\Factory](https://laravel.com/api/laravel/5.6/Illuminate/Contracts/Broadcasting/Factory.html) | ? |
| Broadcast (Instance) | [Illuminate\Contracts\Broadcasting\Broadcaster](https://laravel.com/api/laravel/5.6/Illuminate/Contracts/Broadcasting/Broadcaster.html) | ? |
| Bus | [Illuminate\Contracts\Bus\Dispatcher](https://laravel.com/api/laravel/5.6/Illuminate/Contracts/Bus/Dispatcher.html) | ? |
| Cache | [Illuminate\Cache\CacheManager](https://laravel.com/api/laravel/5.6/Illuminate/Cache/CacheManager.html) | `cache` |
| Cache (Instance) | [Illuminate\Cache\Repository](https://laravel.com/api/laravel/5.6/Illuminate/Cache/Repository.html) | `cache.store` |
| Config | [Illuminate\Config\Repository](https://laravel.com/api/laravel/5.6/Illuminate/Config/Repository.html) | `config` |
| Cookie | [Illuminate\Cookie\CookieJar](https://laravel.com/api/laravel/5.6/Illuminate/Cookie/CookieJar.html) | `cookie` |
| Crypt | [Illuminate\Encryption\Encrypter](https://laravel.com/api/laravel/5.6/Illuminate/Encryption/Encrypter.html) | `encrypter` |
| DB | [Illuminate\Database\DatabaseManager](https://laravel.com/api/laravel/5.6/Illuminate/Database/DatabaseManager.html) | `db` |
| DB (Instance) | [Illuminate\Database\Connection](https://laravel.com/api/laravel/5.6/Illuminate/Database/Connection.html) | `db.connection` |
| Event | [Illuminate\Events\Dispatcher](https://laravel.com/api/laravel/5.6/Illuminate/Events/Dispatcher.html) | `events` |
| File | [Illuminate\Filesystem\Filesystem](https://laravel.com/api/laravel/5.6/Illuminate/Filesystem/Filesystem.html) | `files` |
| Gate | [Illuminate\Contracts\Auth\Access\Gate](https://laravel.com/api/laravel/5.6/Illuminate/Contracts/Auth/Access/Gate.html) | ? |
| Hash | [Illuminate\Contracts\Hashing\Hasher](https://laravel.com/api/laravel/5.6/Illuminate/Contracts/Hashing/Hasher.html) | `hash` |
| Lang | [Illuminate\Translation\Translator](https://laravel.com/api/laravel/5.6/Illuminate/Translation/Translator.html) | `translator` |
| Log | [Illuminate\Log\Logger](https://laravel.com/api/laravel/5.6/Illuminate/Log/Logger.html) | `log` |
| Mail | [Illuminate\Mail\Mailer](https://laravel.com/api/laravel/5.6/Illuminate/Mail/Mailer.html) | `mailer` |
| Notification | [Illuminate\Notifications\ChannelManager](https://laravel.com/api/laravel/5.6/Illuminate/Notifications/ChannelManager.html) | ? |
| Password | [Illuminate\Auth\Passwords\PasswordBrokerManager](https://laravel.com/api/laravel/5.6/Illuminate/Auth/Passwords/PasswordBrokerManager.html) | `auth.password` |
| Password (Instance) | [Illuminate\Auth\Passwords\PasswordBroker](https://laravel.com/api/laravel/5.6/Illuminate/Auth/Passwords/PasswordBroker.html) | `auth.password.broker` |
| Queue | [Illuminate\Queue\QueueManager](https://laravel.com/api/laravel/5.6/Illuminate/Queue/QueueManager.html) | `queue` |
| Queue (Instance) | [Illuminate\Contracts\Queue\Queue](https://laravel.com/api/laravel/5.6/Illuminate/Contracts/Queue/Queue.html) | `queue.connection` |
| Queue (Base Class) | [Illuminate\Queue\Queue](https://laravel.com/api/laravel/5.6/Illuminate/Queue/Queue.html) | ? |
| Redirect | [Illuminate\Routing\Redirector](https://laravel.com/api/laravel/5.6/Illuminate/Routing/Redirector.html) | `redirect` |
| Redis | [Illuminate\Redis\RedisManager](https://laravel.com/api/laravel/5.6/Illuminate/Redis/RedisManager.html) | `redis` |
| Redis (Instance) | [Illuminate\Redis\Connections\Connection](https://laravel.com/api/laravel/5.6/Illuminate/Redis/Connections/Connection.html) | `redis.connection` |
| Request | [Illuminate\Http\Request](https://laravel.com/api/laravel/5.6/Illuminate/Http/Request.html) | `request` |
| Response | [Illuminate\Contracts\Routing\ResponseFactory](https://laravel.com/api/laravel/5.6/Illuminate/Contracts/Routing/ResponseFactory.html) | ? |
| Response (Instance) | [Illuminate\Http\Response](https://laravel.com/api/laravel/5.6/Illuminate/Http/Response.html) | ? |
| Route | [Illuminate\Routing\Router](https://laravel.com/api/laravel/5.6/Illuminate/Routing/Router.html) | `router` |
| Schema | [Illuminate\Database\Schema\Builder](https://laravel.com/api/laravel/5.6/Illuminate/Database/Schema/Builder.html) | ? |
| Session | [Illuminate\Session\SessionManager](https://laravel.com/api/laravel/5.6/Illuminate/Session/SessionManager.html) | `session` |
| Session (Instance) | [Illuminate\Session\Store](https://laravel.com/api/laravel/5.6/Illuminate/Session/Store.html) | `session.store` |
| Storage | [Illuminate\Filesystem\FilesystemManager](https://laravel.com/api/laravel/5.6/Illuminate/Filesystem/FilesystemManager.html) | `filesystem` |
| Storage (Instance) | [Illuminate\Contracts\Filesystem\Filesystem](https://laravel.com/api/laravel/5.6/Illuminate/Contracts/Filesystem/Filesystem.html) | `filesystem.disk` |
| URL | [Illuminate\Routing\UrlGenerator](https://laravel.com/api/laravel/5.6/Illuminate/Routing/UrlGenerator.html) | `url` |
| Validator | [Illuminate\Validation\Factory](https://laravel.com/api/laravel/5.6/Illuminate/Validation/Factory.html) | `validator` |
| Validator (Instance) | [Illuminate\Validation\Validator](https://laravel.com/api/laravel/5.6/Illuminate/Validation/Validator.html) | ? |
| View | [Illuminate\View\Factory](https://laravel.com/api/laravel/5.6/Illuminate/View/Factory.html) | `view` |
| View (Instance) | [Illuminate\View\View](https://laravel.com/api/laravel/5.6/Illuminate/View/View.html) | |
- 前言
- 翻譯說明
- 發行說明
- 升級指南
- 貢獻導引
- 入門指南
- 安裝
- 配置信息
- 文件夾結構
- Homestead
- Valet
- 部署
- 核心架構
- 請求周期
- 服務容器
- 服務提供者
- Facades
- Contracts
- 基礎功能
- 路由
- 中間件
- CSRF 保護
- 控制器
- 請求
- 響應
- 視圖
- URL
- Session
- 表單驗證
- 錯誤
- 日志
- 前端開發
- Blade 模板
- 本地化
- 前端指南
- 編輯資源 Mix
- 安全相關
- 用戶認證
- Passport OAuth 認證
- 用戶授權
- 加密解密
- 哈希
- 重置密碼
- 綜合話題
- Artisan 命令行
- 廣播系統
- 緩存系統
- 集合
- 事件系統
- 文件存儲
- 輔助函數
- 郵件發送
- 消息通知
- 擴展包開發
- 隊列
- 任務調度
- 數據庫
- 快速入門
- 查詢構造器
- 分頁
- 數據庫遷移
- 數據填充
- Redis
- Eloquent ORM
- 快速入門
- 模型關聯
- Eloquent 集合
- 修改器
- API 資源
- 序列化
- 測試相關
- 快速入門
- HTTP 測試
- 瀏覽器測試 Dusk
- 數據庫測試
- 測試模擬器
- 官方擴展包
- Cashier 交易工具包
- Envoy 部署工具
- Horizon
- Scout 全文搜索
- Socialite 社會化登錄