[TOC]
### **1、簡介**
[門面](http://laravelacademy.org/tags/%e9%97%a8%e9%9d%a2 "View all posts in 門面")為應用的[服務容器](http://laravelacademy.org/post/2910.html)中的綁定類提供了一個“靜態”接口。[Laravel](http://laravelacademy.org/tags/laravel "View all posts in Laravel")?內置了很多門面,你可能在不知道的情況下正在使用它們。Laravel 的門面作為[服務容器](http://laravelacademy.org/tags/%e6%9c%8d%e5%8a%a1%e5%ae%b9%e5%99%a8 "View all posts in 服務容器")中的底層類的“靜態代理”,相比于傳統[靜態方法](http://laravelacademy.org/tags/%e9%9d%99%e6%80%81%e6%96%b9%e6%b3%95 "View all posts in 靜態方法"),在維護時能夠提供更加易于測試、更加靈活的、簡明且富有表現力的語法。
### **2、使用門面**
在 Laravel 應用的上下文中,門面就是一個提供訪問容器中對象的類。該機制原理由?`[Facade](http://laravelacademy.org/tags/facade "View all posts in Facade")`?類實現,Laravel 自帶的門面,以及創建的自定義門面,都會繼承自?`Illuminate\Support\Facades\Facade`?基類。
門面類只需要實現一個方法:`getFacadeAccessor`。正是?`getFacadeAccessor`?方法定義了從容器中解析什么,然后`Facade`?基類使用魔術方法?`__callStatic()`?從你的門面中調用解析對象。
下面的例子中,我們將會調用 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`)。
### **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.2/Illuminate/Foundation/Application.html) | `app` |
| Artisan | [Illuminate\Console\Application](http://laravel.com/api/5.2/Illuminate/Console/Application.html) | `artisan` |
| Auth | [Illuminate\Auth\AuthManager](http://laravel.com/api/5.2/Illuminate/Auth/AuthManager.html) | `auth` |
| Auth (Instance) | [Illuminate\Auth\Guard](http://laravel.com/api/5.2/Illuminate/Auth/Guard.html) | |
| Blade | [Illuminate\View\Compilers\BladeCompiler](http://laravel.com/api/5.2/Illuminate/View/Compilers/BladeCompiler.html) | `blade.compiler` |
| Bus | [Illuminate\Contracts\Bus\Dispatcher](http://laravel.com/api/5.2/Illuminate/Contracts/Bus/Dispatcher.html) | |
| Cache | [Illuminate\Cache\Repository](http://laravel.com/api/5.2/Illuminate/Cache/Repository.html) | `cache` |
| Config | [Illuminate\Config\Repository](http://laravel.com/api/5.2/Illuminate/Config/Repository.html) | `config` |
| Cookie | [Illuminate\Cookie\CookieJar](http://laravel.com/api/5.2/Illuminate/Cookie/CookieJar.html) | `cookie` |
| Crypt | [Illuminate\Encryption\Encrypter](http://laravel.com/api/5.2/Illuminate/Encryption/Encrypter.html) | `encrypter` |
| DB | [Illuminate\Database\DatabaseManager](http://laravel.com/api/5.2/Illuminate/Database/DatabaseManager.html) | `db` |
| DB (Instance) | [Illuminate\Database\Connection](http://laravel.com/api/5.2/Illuminate/Database/Connection.html) | |
| Event | [Illuminate\Events\Dispatcher](http://laravel.com/api/5.2/Illuminate/Events/Dispatcher.html) | `events` |
| File | [Illuminate\Filesystem\Filesystem](http://laravel.com/api/5.2/Illuminate/Filesystem/Filesystem.html) | `files` |
| Hash | [Illuminate\Contracts\Hashing\Hasher](http://laravel.com/api/5.2/Illuminate/Contracts/Hashing/Hasher.html) | `hash` |
| Lang | [Illuminate\Translation\Translator](http://laravel.com/api/5.2/Illuminate/Translation/Translator.html) | `translator` |
| Log | [Illuminate\Log\Writer](http://laravel.com/api/5.2/Illuminate/Log/Writer.html) | `log` |
| Mail | [Illuminate\Mail\Mailer](http://laravel.com/api/5.2/Illuminate/Mail/Mailer.html) | `mailer` |
| Password | [Illuminate\Auth\Passwords\PasswordBroker](http://laravel.com/api/5.2/Illuminate/Auth/Passwords/PasswordBroker.html) | `auth.password` |
| Queue | [Illuminate\Queue\QueueManager](http://laravel.com/api/5.2/Illuminate/Queue/QueueManager.html) | `queue` |
| Queue (Instance) | [Illuminate\Queue\QueueInterface](http://laravel.com/api/5.2/Illuminate/Queue/QueueInterface.html) | |
| Queue (Base Class) | [Illuminate\Queue\Queue](http://laravel.com/api/5.2/Illuminate/Queue/Queue.html) | |
| Redirect | [Illuminate\Routing\Redirector](http://laravel.com/api/5.2/Illuminate/Routing/Redirector.html) | `redirect` |
| Redis | [Illuminate\Redis\Database](http://laravel.com/api/5.2/Illuminate/Redis/Database.html) | `redis` |
| Request | [Illuminate\Http\Request](http://laravel.com/api/5.2/Illuminate/Http/Request.html) | `request` |
| Response | [Illuminate\Contracts\Routing\ResponseFactory](http://laravel.com/api/5.2/Illuminate/Contracts/Routing/ResponseFactory.html) | |
| Route | [Illuminate\Routing\Router](http://laravel.com/api/5.2/Illuminate/Routing/Router.html) | `router` |
| Schema | [Illuminate\Database\Schema\Blueprint](http://laravel.com/api/5.2/Illuminate/Database/Schema/Blueprint.html) | |
| Session | [Illuminate\Session\SessionManager](http://laravel.com/api/5.2/Illuminate/Session/SessionManager.html) | `session` |
| Session (Instance) | [Illuminate\Session\Store](http://laravel.com/api/5.2/Illuminate/Session/Store.html) | |
| Storage | [Illuminate\Contracts\Filesystem\Factory](http://laravel.com/api/5.2/Illuminate/Contracts/Filesystem/Factory.html) | `filesystem` |
| URL | [Illuminate\Routing\UrlGenerator](http://laravel.com/api/5.2/Illuminate/Routing/UrlGenerator.html) | `url` |
| Validator | [Illuminate\Validation\Factory](http://laravel.com/api/5.2/Illuminate/Validation/Factory.html) | `validator` |
| Validator (Instance) | [Illuminate\Validation\Validator](http://laravel.com/api/5.2/Illuminate/Validation/Validator.html) | |
| View | [Illuminate\View\Factory](http://laravel.com/api/5.2/Illuminate/View/Factory.html) | `view` |
| View (Instance) | [Illuminate\View\View](http://laravel.com/api/5.2/Illuminate/View/View.html) |
- 序言
- 發行版本說明
- 升級指南
- 貢獻代碼
- 開始
- 安裝
- 配置
- Laravel Homestead
- 基礎
- HTTP 路由
- HTTP 中間件
- HTTP 控制器
- HTTP 請求
- HTTP 響應
- 視圖
- Blade 模板引擎
- 架構
- 一次請求的生命周期
- 應用目錄結構
- 服務提供者
- 服務容器
- 門面(Facades)
- 數據庫
- 起步
- 查詢構建器
- 遷移
- 填充數據
- Eloquent ORM
- 起步
- 關聯關系
- 集合
- 訪問器&修改器
- 序列化
- 服務
- 用戶認證
- 用戶授權
- Artisan Console
- 訂閱支付實現:Laravel Cashier
- 緩存
- 集合
- 集成前端資源:Laravel Elixir
- 加密
- 錯誤&日志
- 事件
- 文件系統/云存儲
- 哈希
- 輔助函數
- 本地化
- 郵件
- 包開發
- 分頁
- Redis
- 隊列
- Session
- Envoy Task Runner
- 任務調度
- 測試
- 驗證
- 新手入門指南
- 簡單任務管理系統
- 帶用戶功能的任務管理系統