# Facades
## 簡介
>[success]Facades 為應用的[服務容器]提供了一個「靜態」 接口。Laravel 自帶了很多 Facades,可以訪問絕大部分功能。`Laravel Facades`實際是服務容器中底層類的 「靜態代理」 ,相對于傳統靜態方法,在使用時能夠提供更加靈活、更加易于測試、更加優雅的語法
所有的 Laravel Facades 都定義在`Illuminate\Support\Facades`命名空間下。所以,我們可以輕松的使用 Facade :
~~~php
use Illuminate\Support\Facades\Cache;
Route::get('/cache', function () {
return Cache::get('key');
});
~~~
在 Laravel 文檔中,有很多示例代碼都會使用 Facades 來演示框架的各種功能
## 何時使用 Facades
>[success]Facades 有很多優點,它提供了簡單,易記的語法,從而無需手動注入或配置長長的類名。此外,由于他們對`PHP`靜態方法的獨特調用,使得測試起來非常容易。
然而,在使用 Facades 時,有些地方需要特別注意。使用 Facades 時最主要的危險就是會引起類作用范圍的膨脹。由于 Facades 使用起來非常簡單并且不需要注入,就會使得我們不經意間在單個類中使用許多 Facades ,從而導致類變得越來越大。然而使用依賴注入的時候,使用的類越多,構造方法就會越長,在視覺上注意到這個類有些龐大了。因此在使用 Facades 的時候,要特別注意控制類的大小,讓類的作用范圍保持短小。
> {tip} 在開發與 Laravel 進行交互的第三方擴展包時,最好選擇注入[Laravel 契約](https://laravel-china.org/docs/laravel/5.7/contracts)而不使用 Facades 。因為擴展包是在 Laravel 之外構建,你無法使用 Laravel Facades 測試輔助函數
### Facades Vs. 依賴注入
>[success]依賴注入的主要優點之一是切換注入類的實現。這在測試的時候很有用,因為你可以注入一個 模擬或`stub`,并斷言`stub`上調出各種方法。
通常,真正的靜態方法是不可能模擬或`stub`的。但是 Facades 使用動態方法對服務容器中解析出來的對象方法的調用進行了代理,我們也可以像測試注入類實例一樣測試 Facades。比如,像下面的路由:
~~~php
use Illuminate\Support\Facades\Cache;
Route::get('/cache', function () {
return Cache::get('key');
});
~~~
我們可以帶上我們期望的參數編寫下面的測試代碼來驗證`Cache::get`方法:
~~~php
use Illuminate\Support\Facades\Cache;
/**
* 一個基礎功能的測試
*
* @return void
*/
public function testBasicExample()
{
Cache::shouldReceive('get')
->with('key')
->andReturn('value');
$this->visit('/cache')
->see('value');
}
~~~
### Facades Vs. 輔助函數
>[success]除了 Facades,Laravel 還包含各種 『輔助函數』 來實現這些常用功能,比如生成視圖、觸發事件、任務調度或者發送 HTTP 響應。許多輔助函數都有與之對應的 Facades 。例如,下面這個 Facades 和輔助函數的作用是一樣的:
~~~php
return View::make('profile');
return view('profile');
~~~
Facade 和輔助函數之間沒有實際的區別。當你使用輔助函數時,你可以像測試相應的 Facade 那樣進行測試。例如,下面的路由:
~~~php
Route::get('/cache', function () {
return cache('key');
});
~~~
在底層實現,輔助函數`cache`實際是調用`Cache`這個 Facade 的`get`方法。因此,盡管我們使用的是輔助函數,我們依然可以帶上我們期望的參數編寫下面的測試代碼來驗證`Cache::get`方法:
~~~php
use Illuminate\Support\Facades\Cache;
/**
* 一個基礎功能的測試用例
*
* @return void
*/
public function testBasicExample()
{
Cache::shouldReceive('get')
->with('key')
->andReturn('value');
$this->visit('/cache')
->see('value');
}
~~~
## Facades 工作原理
>[success]在 Laravel 應用中,Facade 就是一個可以從容器訪問對象的類。其中核心的部件就是`Facade`類。不管是 Laravel 自帶的 Facades,還是自定義的 Facades,都繼承自`Illuminate\Support\Facades\Facade`類。
`Facade`基類使用了`__callStatic()`魔術方法,直到對象從容器中被解析出來后,才會進行調用。在下面的例子中,調用了 Laravel 的緩存系統。通過瀏覽這段代碼,可以假定在`Cache`類中調用了靜態方法`get`:
~~~php
<?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`這個靜態方法:
~~~php
class Cache extends Facade
{
/**
* 獲取組件的注冊名稱。
*
* @return string
*/
protected static function getFacadeAccessor() { return 'cache'; }
}
~~~
`Cache`Facade 繼承了`Facade`類,并且定義了`getFacadeAccessor()`方法。這個方法的作用是返回服務容器綁定的名稱。當用戶調用`Cache`Facade 中的任何靜態方法時,Laravel 會從[服務容器](https://laravel-china.org/docs/laravel/5.7/container)中解析`cache`綁定以及該對象運行所請求的方法(在這個例子中就是`get`方法)。
## 實時 Facades
>[success]使用實時 Facades,你可以將應用程序中的任何類視為 Facade。為了說明這是如何使用的,我們來看看另一種方法。例如,假設我們的`Podcast`模型有一個`publish`方法。然而,為了發布 Podcast,我們需要注入一個`Publisher`實例:
~~~php
<?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);
}
}
~~~
>[info]將發布者的實現注入到該方法中,我們可以輕松地測試這種方法,因為我們可以模擬注入的發布者。但是,它要求我們每次調用`publish`方法時都要傳遞一個發布者實例。使用實時的 Facades,我們可以保持同樣的可測試性,而不需要顯式地通過`Publisher`實例。要生成實時Facade,請在導入類的名稱空間中加上`Facades`:
~~~php
<?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
<?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 文檔的工具。[服務容器綁定](https://laravel-china.org/docs/laravel/5.7/container)的關鍵信息也包含在內。
| Facade | Class | Service Container Binding |
| --- | --- | --- |
| App | [Illuminate\\Foundation\\Application](https://laravel.com/api/laravel/5.7/Illuminate/Foundation/Application.html) | `app` |
| Artisan | [Illuminate\\Contracts\\Console\\Kernel](https://laravel.com/api/laravel/5.7/Illuminate/Contracts/Console/Kernel.html) | `artisan` |
| Auth | [Illuminate\\Auth\\AuthManager](https://laravel.com/api/laravel/5.7/Illuminate/Auth/AuthManager.html) | `auth` |
| Auth (Instance) | [Illuminate\\Contracts\\Auth\\Guard](https://laravel.com/api/laravel/5.7/Illuminate/Contracts/Auth/Guard.html) | `auth.driver` |
| Blade | [Illuminate\\View\\Compilers\\BladeCompiler](https://laravel.com/api/laravel/5.7/Illuminate/View/Compilers/BladeCompiler.html) | `blade.compiler` |
| Broadcast | [Illuminate\\Contracts\\Broadcasting\\Factory](https://laravel.com/api/laravel/5.7/Illuminate/Contracts/Broadcasting/Factory.html) | ? |
| Broadcast (Instance) | [Illuminate\\Contracts\\Broadcasting\\Broadcaster](https://laravel.com/api/laravel/5.7/Illuminate/Contracts/Broadcasting/Broadcaster.html) | ? |
| Bus | [Illuminate\\Contracts\\Bus\\Dispatcher](https://laravel.com/api/laravel/5.7/Illuminate/Contracts/Bus/Dispatcher.html) | ? |
| Cache | [Illuminate\\Cache\\CacheManager](https://laravel.com/api/laravel/5.7/Illuminate/Cache/CacheManager.html) | `cache` |
| Cache (Instance) | [Illuminate\\Cache\\Repository](https://laravel.com/api/laravel/5.7/Illuminate/Cache/Repository.html) | `cache.store` |
| Config | [Illuminate\\Config\\Repository](https://laravel.com/api/laravel/5.7/Illuminate/Config/Repository.html) | `config` |
| Cookie | [Illuminate\\Cookie\\CookieJar](https://laravel.com/api/laravel/5.7/Illuminate/Cookie/CookieJar.html) | `cookie` |
| Crypt | [Illuminate\\Encryption\\Encrypter](https://laravel.com/api/laravel/5.7/Illuminate/Encryption/Encrypter.html) | `encrypter` |
| DB | [Illuminate\\Database\\DatabaseManager](https://laravel.com/api/laravel/5.7/Illuminate/Database/DatabaseManager.html) | `db` |
| DB (Instance) | [Illuminate\\Database\\Connection](https://laravel.com/api/laravel/5.7/Illuminate/Database/Connection.html) | `db.connection` |
| Event | [Illuminate\\Events\\Dispatcher](https://laravel.com/api/laravel/5.7/Illuminate/Events/Dispatcher.html) | `events` |
| File | [Illuminate\\Filesystem\\Filesystem](https://laravel.com/api/laravel/5.7/Illuminate/Filesystem/Filesystem.html) | `files` |
| Gate | [Illuminate\\Contracts\\Auth\\Access\\Gate](https://laravel.com/api/laravel/5.7/Illuminate/Contracts/Auth/Access/Gate.html) | ? |
| Hash | [Illuminate\\Contracts\\Hashing\\Hasher](https://laravel.com/api/laravel/5.7/Illuminate/Contracts/Hashing/Hasher.html) | `hash` |
| Lang | [Illuminate\\Translation\\Translator](https://laravel.com/api/laravel/5.7/Illuminate/Translation/Translator.html) | `translator` |
| Log | [Illuminate\\Log\\Logger](https://laravel.com/api/laravel/5.7/Illuminate/Log/Logger.html) | `log` |
| Mail | [Illuminate\\Mail\\Mailer](https://laravel.com/api/laravel/5.7/Illuminate/Mail/Mailer.html) | `mailer` |
| Notification | [Illuminate\\Notifications\\ChannelManager](https://laravel.com/api/laravel/5.7/Illuminate/Notifications/ChannelManager.html) | ? |
| Password | [Illuminate\\Auth\\Passwords\\PasswordBrokerManager](https://laravel.com/api/laravel/5.7/Illuminate/Auth/Passwords/PasswordBrokerManager.html) | `auth.password` |
| Password (Instance) | [Illuminate\\Auth\\Passwords\\PasswordBroker](https://laravel.com/api/laravel/5.7/Illuminate/Auth/Passwords/PasswordBroker.html) | `auth.password.broker` |
| Queue | [Illuminate\\Queue\\QueueManager](https://laravel.com/api/laravel/5.7/Illuminate/Queue/QueueManager.html) | `queue` |
| Queue (Instance) | [Illuminate\\Contracts\\Queue\\Queue](https://laravel.com/api/laravel/5.7/Illuminate/Contracts/Queue/Queue.html) | `queue.connection` |
| Queue (Base Class) | [Illuminate\\Queue\\Queue](https://laravel.com/api/laravel/5.7/Illuminate/Queue/Queue.html) | ? |
| Redirect | [Illuminate\\Routing\\Redirector](https://laravel.com/api/laravel/5.7/Illuminate/Routing/Redirector.html) | `redirect` |
| Redis | [Illuminate\\Redis\\RedisManager](https://laravel.com/api/laravel/5.7/Illuminate/Redis/RedisManager.html) | `redis` |
| Redis (Instance) | [Illuminate\\Redis\\Connections\\Connection](https://laravel.com/api/laravel/5.7/Illuminate/Redis/Connections/Connection.html) | `redis.connection` |
| Request | [Illuminate\\Http\\Request](https://laravel.com/api/laravel/5.7/Illuminate/Http/Request.html) | `request` |
| Response | [Illuminate\\Contracts\\Routing\\ResponseFactory](https://laravel.com/api/laravel/5.7/Illuminate/Contracts/Routing/ResponseFactory.html) | ? |
| Response (Instance) | [Illuminate\\Http\\Response](https://laravel.com/api/laravel/5.7/Illuminate/Http/Response.html) | ? |
| Route | [Illuminate\\Routing\\Router](https://laravel.com/api/laravel/5.7/Illuminate/Routing/Router.html) | `router` |
| Schema | [Illuminate\\Database\\Schema\\Builder](https://laravel.com/api/laravel/5.7/Illuminate/Database/Schema/Builder.html) | ? |
| Session | [Illuminate\\Session\\SessionManager](https://laravel.com/api/laravel/5.7/Illuminate/Session/SessionManager.html) | `session` |
| Session (Instance) | [Illuminate\\Session\\Store](https://laravel.com/api/laravel/5.7/Illuminate/Session/Store.html) | `session.store` |
| Storage | [Illuminate\\Filesystem\\FilesystemManager](https://laravel.com/api/laravel/5.7/Illuminate/Filesystem/FilesystemManager.html) | `filesystem` |
| Storage (Instance) | [Illuminate\\Contracts\\Filesystem\\Filesystem](https://laravel.com/api/laravel/5.7/Illuminate/Contracts/Filesystem/Filesystem.html) | `filesystem.disk` |
| URL | [Illuminate\\Routing\\UrlGenerator](https://laravel.com/api/laravel/5.7/Illuminate/Routing/UrlGenerator.html) | `url` |
| Validator | [Illuminate\\Validation\\Factory](https://laravel.com/api/laravel/5.7/Illuminate/Validation/Factory.html) | `validator` |
| Validator (Instance) | [Illuminate\\Validation\\Validator](https://laravel.com/api/laravel/5.7/Illuminate/Validation/Validator.html) | ? |
| View | [Illuminate\\View\\Factory](https://laravel.com/api/laravel/5.7/Illuminate/View/Factory.html) | `view` |
| View (Instance) | [Illuminate\\View\\View](https://laravel.com/api/laravel/5.7/Illuminate/View/View.html) |