🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[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) |