Laravel5.1学习笔记i14 系统架构6 Facade
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 baseIlluminate\Support\Facades\Facade
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类,将会继承基类 Illuminate\Support\Facades\Facade
class.
一个facade类只需要去实现一个方法: getFacadeAccessor。 getFacadeAccessor方法的工作是定义要从容器解析什么。 基类Facade利用_callStatic 魔术方法来从你的facade调用到解析出来的对象。
在下面的例子中, 产生一个对Laravel的Cache系统 的调用。 审视一下这个代码,可能你会以为一个Cache类的静态方法get被调用。
<?php namespace App\Http\Controllers; use Cache;
use App\Http\Controllers\Controller; 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 Illuminate\Contracts\Cache\Factory
interface. Any calls we make using the facade will be passed to the underlying instance of Laravel's cache service.
请注意在代码的顶部,我们引入了Cache facade,这个facade作为代理去获取底层 Illuminate\Contracts\Cache\Factory 接口的实现, 所有对facade的调用会传入到底层 Laravel的Cache服务的实例。
If we look at that Illuminate\Support\Facades\Cache
class, you'll see that there is no static method get
:
如果我们看这个Illuminate\Support\Facades\Cache
类, 我们将看不到任何静态方法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根, 可以快速深入文档的有用工具, 服务容器绑定也包括在内
Laravel5.1学习笔记i14 系统架构6 Facade的更多相关文章
- Laravel5.1学习笔记12 系统架构4 服务容器
Service Container 介绍 绑定的用法 绑定实例到接口 上下文绑定 标签 解析 容器事件 #介绍 The Laravel service container is a powerful ...
- Laravel5.1学习笔记13 系统架构5 Contract
Contract 简介 为什么要用 Contract? Contract 参考 如何使用 Contract 简介 Laravel 中的 Contract 是一组定义了框架核心服务的接口.例如,Illu ...
- Laravel5.1学习笔记11 系统架构3 服务提供者
服务提供者 简介 写一个服务提供者 Register注册方法 Boot 方法 注册提供者 缓载提供者 简介 Service providers are the central place of all ...
- Laravel5.1学习笔记10 系统架构2 应用程序结构
应用程序结构 简介 根目录 App 目录 为应用程序设置命名空间 简介 默认的 Laravel 应用程序结构是为了给无论构建大型还是小型应用程序都提供一个良好的开始.当然,你可以依照喜好自由地组织应用 ...
- Laravel5.1学习笔记9 系统架构1 请求生命周期 (待修)
Request Lifecycle Introduction Lifecycle Overview Focus On Service Providers Introduction When using ...
- ODI学习笔记2--ODI产品架构
ODI学习笔记2--ODI产品架构 ODI产品架构: ODI提供了以下几种管理工具:Designer 用于定义数据转换逻辑,这是最常用的开发工具,大部分的开发任务,包括data store的定义,in ...
- Symfony2 学习笔记之系统路由
mfony2 学习笔记之系统路由 漂亮的URL绝对是一个严肃的web应用程序必须做到的,这种方式使index.php?article_id=57这类的丑陋URL被隐藏,由更受欢迎的像 /read/ ...
- Linux学习笔记-Linux系统简介
Linux学习笔记-Linux系统简介 UNIX与Linux发展史 UNIX是父亲,Linux是儿子. UNIX发行版本 操作系统 公司 硬件平台 AIX IBM PowerPC HP-UX HP P ...
- 源码学习之Spring (系统架构简单解析)
Spring Framework 系统架构总览图 Spring Framework的模块依赖关系图 Spring Framework各个模块功能说明 Spring核心模块 模块名称 主要功能 Spri ...
随机推荐
- DESEncrypt对称加密解密
分享一个很好用的DESEncrypt对称加密解密的类 using System; using System.Security.Cryptography; using System.Text; usin ...
- 小数化分数的O(log2n)解法
具体约束: 给定一个小数x,x满足0<=x<1,且保证给定的x保留了18位小数 输出一个分数,使得分母不超过1e9,分子分母互质,且在满足这些条件的情况下最接近x 了解一下法雷数列和ste ...
- Maven学习总结(29)——Maven项目的pom.xml中log4j2配置
<dependency> <groupId>org.apache.logging.log4j</groupId> <a ...
- Cow Sorting POJ 3270 & HDU 2838
题目网址:http://poj.org/problem?id=3270 题目大意是:一串无序的数字,要排成增序的数列,可以交换不相邻的数,每交换两个数,sum+这两个数,使得sum最小,求最小的sum ...
- Eclipse不编译解决方案
原文链接:http://blog.csdn.net/huahuagongzi99999/article/details/7719882 转来自己用 这两天Eclipse 不编译了,无论怎么更改保 ...
- 中庸之道(codevs 2021)
题目描述 Description 给定一个长度为N的序列,有Q次询问,每次询问区间[L,R]的中位数. 数据保证序列中任意两个数不相同,且询问的所有区间长度为奇数. 输入描述 Input Descri ...
- MyBatis3-示例工程
一.准备工作: 0.新建QuitStart类型POM项目(即Application),Java Build Path为JDK1.8,Java Compiler为1.8,MySQL为5.5.38,数据库 ...
- 【Nginx】进程模型
转自:网易博客 服务器的并发模型设计是网络编程中很关键的一个部分,服务器的并发量取决于两个因素,一个是提供服务的进程数量,另外一个是每个进程可同时处理的并发连接数量.相应的,服务器的并发模型也由两个部 ...
- IT学子成长指导类文章链接(十二)
链接:IT学子成长指导类文章链接(一)(二)(三) (四) (五)(六)(七)(八)(九)(十)(十一) "IT学子成长指导"类我收藏过的好文(十二期:至2014年6月17日) 程 ...
- LINKs: Xamarin.Forms + Prism
LINK 1 - How to use Prism with Xamarin.Forms http://brianlagunas.com/first-look-at-the-prism-for-xam ...