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 Facadebase 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\Factoryinterface. 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 ...
随机推荐
- 【03】全局 CSS 样式
全局 CSS 样式 设置全局 CSS 样式:基本的 HTML 元素均可以通过 class 设置样式并得到增强效果:还有先进的栅格系统. 概览 深入了解 Bootstrap 底层结构的关键部分,包括我们 ...
- 【Lqb T336】Cowboys
[链接] 我是链接,点我呀:) [题意] 出现AB就要交换一下 给你结果序列 问你原序列有多少种可能 首尾可以交换. [题解] 设 dp[i][0]表示i和i-1不交换,达到前i个字符序列的状态的方案 ...
- BZOJ3238:[AHOI 2013]差异
求一个字符串的∑ ∑ len[i] + len[j] - 2 * lcp(i, j),其中i,j表示从i,j开始的后缀. 方法一:SA+单调栈,自行yy. 方法二:SAM构造出来,然后每个状态对答案的 ...
- ArcGIS AO中控制图层中要素可见状态的总结
一.DefinitionExpression 实现新建查询图层,查询结果要素为选中状态 该接口可以通过两种方法来控制要素的可见状态. 思路1 通过该接口的 DefinitionExpression 方 ...
- sqlalchemy foreign key查询和backref
首先在mysql中创建两个表如下: mysql) , primary key(id)); Query OK, rows affected (0.01 sec) mysql),user_id int, ...
- [Vue @Component] Simplify Vue Components with vue-class-component
While traditional Vue components require a data function which returns an object and a method object ...
- 一起talk C栗子吧(第一百二十三回:C语言实例--显示变量和函数的地址)
各位看官们,大家好,上一回中咱们说的是多线程的样例.这一回咱们说的样例是:显示变量和函数的地址. 闲话休提,言归正转.让我们一起talk C栗子吧! 在编敲代码时,有时候须要获取程序中变量和函数的地址 ...
- PHP移动互联网开发笔记(3)——运算符
原文地址:http://www.php100.com/html/php/rumen/2014/0326/6704.html 一.PHP的运算符 PHP中有丰富的运算符集,它们中大部分直接来自于C语言. ...
- Linux Framebuffer驱动剖析之中的一个—软件需求
嵌入式企鹅圈将以本文作为2015年的终结篇,以回应第一篇<Linux字符设备驱动剖析>.嵌入式企鹅圈一直专注于嵌入式Linux和物联网IOT双方面的原创技术分享,稍后会公布嵌入式企鹅圈的2 ...
- Android中静态变量的生命周期
静态变量的生命周期,起始于类的加载,终止于类的释放.什么时候类会加载呢?我们知道,在app打开时,会创建一个进程,然后初始化一个dvm的实例,负责类的加载释放 和 垃圾回收等.换句话说,在进程创建之后 ...