ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 架构 —— 门面(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) |