# 架構 —— 門面(Facades)
## 1、簡介
門面為應用的服務容器中的有效類提供了一個“靜態”接口。Laravel附帶了很多門面,你可能在不知道的情況下正在使用它們。Laravel的門面作為服務容器中的底層類的“靜態代理”,相比于傳統靜態方法,在維護時能夠提供更加易于測試、更加靈活的、簡明且富有表現力的語法。
## 2、使用門面
在Laravel應用的上下文中,門面就是一個提供訪問容器中對象的類。該機制原理由`Facade`類實現,Laravel自帶的門面,以及創建的自定義門面,都會繼承自`Illuminate\Support\Facades\Facade`基類。
門面類只需要實現一個方法:`getFacadeAccessor`。正是`getFacadeAccessor`方法定義了從容器中解析什么,然后`Facade`基類使用魔術方法從你的門面中調用解析對象。
下面的例子中,我們將會調用Laravel的緩存系統,瀏覽代碼后,也許你會覺得我們調用了`Cache`的靜態方法`get`:
~~~
<?php
namespace App\Http\Controllers;
use Cache;
use App\Http\Controllers\Controller;
class UserController extends Controller{
/**
* 為指定用戶顯示屬性
*
* @param int $id
* @return Response
*/
public function showProfile($id)
{
$user = Cache::get('user:'.$id);
return view('profile', ['user' => $user]);
}
}
~~~
注意我們在頂部位置引入了Cache門面。該門面作為代理訪問底層`Illuminate\Contracts\Cache\Factory`接口的實現。我們對門面的所有調用都會被傳遞給Laravel緩存服務的底層實例。
如果我們查看`Illuminate\Support\Facades\Cache`類的源碼,將會發現其中并沒有靜態方法`get`:
~~~
class Cache extends Facade{
/**
* 獲取組件注冊名稱
*
* @return string
*/
protected static function getFacadeAccessor() {
return 'cache';
}
}
~~~
`Cache`門面繼承`Facade`基類并定義了`getFacadeAccessor`方法,該方法的工作就是返回服務容器綁定類的別名,當用戶引用`Cache`類的任何靜態方法時,Laravel從服務容器中解析cache綁定,然后在解析出的對象上調用所有請求方法(本例中是get)。
> 擴展閱讀:[實例教程 —— 創建自定義Facades類](http://laravelacademy.org/post/817.html)
## 3、門面類列表
下面列出了每個門面及其對應的底層類,這對深入給定根門面的API[文檔](http://laravelacademy.org/tags/%e6%96%87%e6%a1%a3 "View all posts in 文檔")而言是個很有用的工具。服務容器綁定鍵也被包含進來:
| 門面 | 類 | 服務容器綁定別名 |
| --- | --- | --- |
| App | [Illuminate\Foundation\Application](http://laravel.com/api/5.1/Illuminate/Foundation/Application.html) | `app` |
| Artisan | [Illuminate\Console\Application](http://laravel.com/api/5.1/Illuminate/Console/Application.html) | `artisan` |
| Auth | [Illuminate\Auth\AuthManager](http://laravel.com/api/5.1/Illuminate/Auth/AuthManager.html) | `auth` |
| Auth (Instance) | [Illuminate\Auth\Guard](http://laravel.com/api/5.1/Illuminate/Auth/Guard.html) | |
| Blade | [Illuminate\View\Compilers\BladeCompiler](http://laravel.com/api/5.1/Illuminate/View/Compilers/BladeCompiler.html) | `blade.compiler` |
| Bus | [Illuminate\Contracts\Bus\Dispatcher](http://laravel.com/api/5.1/Illuminate/Contracts/Bus/Dispatcher.html) | |
| Cache | [Illuminate\Cache\Repository](http://laravel.com/api/5.1/Illuminate/Cache/Repository.html) | `cache` |
| Config | [Illuminate\Config\Repository](http://laravel.com/api/5.1/Illuminate/Config/Repository.html) | `config` |
| Cookie | [Illuminate\Cookie\CookieJar](http://laravel.com/api/5.1/Illuminate/Cookie/CookieJar.html) | `cookie` |
| Crypt | [Illuminate\Encryption\Encrypter](http://laravel.com/api/5.1/Illuminate/Encryption/Encrypter.html) | `encrypter` |
| DB | [Illuminate\Database\DatabaseManager](http://laravel.com/api/5.1/Illuminate/Database/DatabaseManager.html) | `db` |
| DB (Instance) | [Illuminate\Database\Connection](http://laravel.com/api/5.1/Illuminate/Database/Connection.html) | |
| Event | [Illuminate\Events\Dispatcher](http://laravel.com/api/5.1/Illuminate/Events/Dispatcher.html) | `events` |
| File | [Illuminate\Filesystem\Filesystem](http://laravel.com/api/5.1/Illuminate/Filesystem/Filesystem.html) | `files` |
| Hash | [Illuminate\Contracts\Hashing\Hasher](http://laravel.com/api/5.1/Illuminate/Contracts/Hashing/Hasher.html) | `hash` |
| Input | [Illuminate\Http\Request](http://laravel.com/api/5.1/Illuminate/Http/Request.html) | `request` |
| Lang | [Illuminate\Translation\Translator](http://laravel.com/api/5.1/Illuminate/Translation/Translator.html) | `translator` |
| Log | [Illuminate\Log\Writer](http://laravel.com/api/5.1/Illuminate/Log/Writer.html) | `log` |
| Mail | [Illuminate\Mail\Mailer](http://laravel.com/api/5.1/Illuminate/Mail/Mailer.html) | `mailer` |
| Password | [Illuminate\Auth\Passwords\PasswordBroker](http://laravel.com/api/5.1/Illuminate/Auth/Passwords/PasswordBroker.html) | `auth.password` |
| Queue | [Illuminate\Queue\QueueManager](http://laravel.com/api/5.1/Illuminate/Queue/QueueManager.html) | `queue` |
| Queue (Instance) | [Illuminate\Queue\QueueInterface](http://laravel.com/api/5.1/Illuminate/Queue/QueueInterface.html) | |
| Queue (Base Class) | [Illuminate\Queue\Queue](http://laravel.com/api/5.1/Illuminate/Queue/Queue.html) | |
| Redirect | [Illuminate\Routing\Redirector](http://laravel.com/api/5.1/Illuminate/Routing/Redirector.html) | `redirect` |
| Redis | [Illuminate\Redis\Database](http://laravel.com/api/5.1/Illuminate/Redis/Database.html) | `redis` |
| Request | [Illuminate\Http\Request](http://laravel.com/api/5.1/Illuminate/Http/Request.html) | `request` |
| Response | [Illuminate\Contracts\Routing\ResponseFactory](http://laravel.com/api/5.1/Illuminate/Contracts/Routing/ResponseFactory.html) | |
| Route | [Illuminate\Routing\Router](http://laravel.com/api/5.1/Illuminate/Routing/Router.html) | `router` |
| Schema | [Illuminate\Database\Schema\Blueprint](http://laravel.com/api/5.1/Illuminate/Database/Schema/Blueprint.html) | |
| Session | [Illuminate\Session\SessionManager](http://laravel.com/api/5.1/Illuminate/Session/SessionManager.html) | `session` |
| Session (Instance) | [Illuminate\Session\Store](http://laravel.com/api/5.1/Illuminate/Session/Store.html) | |
| Storage | [Illuminate\Contracts\Filesystem\Factory](http://laravel.com/api/5.1/Illuminate/Contracts/Filesystem/Factory.html) | `filesystem` |
| URL | [Illuminate\Routing\UrlGenerator](http://laravel.com/api/5.1/Illuminate/Routing/UrlGenerator.html) | `url` |
| Validator | [Illuminate\Validation\Factory](http://laravel.com/api/5.1/Illuminate/Validation/Factory.html) | `validator` |
| Validator (Instance) | [Illuminate\Validation\Validator](http://laravel.com/api/5.1/Illuminate/Validation/Validator.html) | |
| View | [Illuminate\View\Factory](http://laravel.com/api/5.1/Illuminate/View/Factory.html) | `view` |
| View (Instance) | [Illuminate\View\View](http://laravel.com/api/5.1/Illuminate/View/View.html) |
- 前言
- 序言
- 序言 ―― 發行版本說明
- 序言 ―― 升級指南
- 序言 ―― 貢獻代碼
- 開始
- 開始 ―― 安裝及配置
- 開始 ―― Laravel Homestead
- 基礎
- 基礎 ―― HTTP路由
- 基礎 ―― HTTP 中間件
- 基礎 ―― HTTP 控制器
- 基礎 ―― HTTP 請求
- 基礎 ―― HTTP 響應
- 基礎 ―― 視圖
- 基礎 ―― Blade模板
- 架構
- 架構 ―― 一次請求的生命周期
- 架構 ―― 應用目錄結構
- 架構 ―― 服務提供者
- 架構 ―― 服務容器
- 架構 ―― 契約
- 架構 ―― 門面
- 數據庫
- 數據庫 ―― 起步
- 數據庫 ―― 查詢構建器
- 數據庫 ―― 遷移
- 數據庫 ―― 填充數據
- Eloquent ORM
- Eloquent ORM ―― 起步
- Eloquent ORM ―― 關聯關系
- Eloquent ORM ―― 集合
- Eloquent ORM ―― 調整器
- Eloquent ORM ―― 序列化
- 服務
- 服務 ―― 用戶認證
- 服務 ―― Artisan 控制臺
- 服務 ―― Laravel Cashier(交易)
- 服務 ―― 緩存
- 服務 ―― 集合
- 服務 ―― Laravel Elixir
- 服務 ―― 加密
- 服務 ―― 錯誤&日志
- 服務 ―― 事件
- 服務 ―― 文件系統/云存儲
- 服務 ―― 哈希
- 服務 ―― 幫助函數
- 服務 ―― 本地化
- 服務 ―― 郵件
- 服務 ―― 包開發
- 服務 ―― 分頁
- 服務 ―― 隊列
- 服務 ―― Redis
- 服務 ―― Session
- 服務 ―― Envoy 任務運行器(SSH任務)
- 服務 ―― 任務調度
- 服務 ―― 測試
- 服務 ―― 驗證