Facades
#介绍
Facades provide a "static" interface to classes that are available in the application's service container. Laravel ships with many facades, and you have probably been using them without even knowing it! Laravel "facades" serve as "static proxies" to underlying classes in the service container, providing the benefit of a terse, expressive syntax while maintaining more testability and flexibility than traditional static methods.
Facades 提供一个静态接口给在应用程序的 服务容器 可以取用的类. Laravel 附带许多facades, 甚至你可能已经在不知情的情况下使用了它们! Laravel的 Facades 作为在IOC容器里面的基础类的静态代理,提供的语法有简洁,易表达的优点, 同时维持比传统的静态方法更高的可测试性和弹性。
#使用Facade
In the context of a Laravel application, a facade is a class that provides access to an object from the container. The machinery that makes this work is in the Facade
class. Laravel's facades, and any custom facades you create, will extend the baseIlluminateSupportFacadesFacade
class.
A facade class only needs to implement a single method: getFacadeAccessor
. It's thegetFacadeAccessor
method's job to define what to resolve from the container. The Facade
base class makes use of the __callStatic()
magic-method to defer calls from your facade to the resolved object.
In the example below, a call is made to the Laravel cache system. By glancing at this code, one might assume that the static method get
is being called on the Cache
class:
在Laravel应用的环境中,facade是个提供从容器访问对象的类。Facade类是让这个机制可以运作的原因。 Laravel 的facades和你建立的任何自定义的facades类,将会继承基类 IlluminateSupportFacadesFacade
class.
一个facade类只需要去实现一个方法: getFacadeAccessor。 getFacadeAccessor方法的工作是定义要从容器解析什么。 基类Facade利用_callStatic 魔术方法来从你的facade调用到解析出来的对象。
在下面的例子中, 产生一个对Laravel的Cache系统 的调用。 审视一下这个代码,可能你会以为一个Cache类的静态方法get被调用。
<?php
namespace AppHttpControllers;
use Cache;
use AppHttpControllersController;
class UserController extends Controller
{
/**
* Show the profile for the given user.
*
* @param int $id
* @return Response
*/
public function showProfile($id)
{
$user = Cache::get('user:'.$id');
return view('profile', ['user' => $user]);
}
}
Notice that near the top of the file we are "importing" the Cache
facade. This facade serves as a proxy to accessing the underlying implementation of the IlluminateContractsCacheFactory
interface. Any calls we make using the facade will be passed to the underlying instance of Laravel's cache service.
请注意在代码的顶部,我们引入了Cache facade,这个facade作为代理去获取底层 IlluminateContractsCacheFactory 接口的实现, 所有对facade的调用会传入到底层 Laravel的Cache服务的实例。
If we look at that IlluminateSupportFacadesCache
class, you'll see that there is no static method get
:
如果我们看这个IlluminateSupportFacadesCache
类, 我们将看不到任何静态方法get:
class Cache extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() { return 'cache'; }
}
Instead, the Cache
facade extends the base Facade
class and defines the methodgetFacadeAccessor()
. Remember, this method's job is to return the name of a service container binding. When a user references any static method on the Cache
facade, Laravel resolves the cache
binding from the service container and runs the requested method (in this case, get
) against that object.
相反,Cache facade继承基类Facade,并定义一个个头getFacadeAccessor 方法,记住,这个方法的任务是返回服务容器绑定的名称, 当用户在Cache facade上引用任何方法, Laravel会从服务容器中解析被绑定的 cache, 然后对该对象执行被请求的方法(在这个例子中 是 get)
#Facade 类的参考
Below you will find every facade and its underlying class. This is a useful tool for quickly digging into the API documentation for a given facade root. The service container bindingkey is also included where applicable.
下面你将会找到每个facade 和他的支持类,这是一个对于给定的facade根, 可以快速深入文档的有用工具, 服务容器绑定也包括在内