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 ...
随机推荐
- RequestMapping_HiddenHttpMethodFilter 过滤器
[REST] 1.REST:即Representational State Transfer.(资源)表现层状态转化.是目前最流行的一种互联网软件架构.它结构清晰.符合标准.易于理解.扩展方便,所以得 ...
- 【05】JSON笔记
[05]笔记 尽管有许多宣传关于 XML 如何拥有跨平台,跨语言的优势,然而,除非应用于 Web Services,否则,在普通的 Web 应用中,开发者经常为 XML 的解析伤透 ...
- 使用lombok提高编码效率-----不用写get set
使用lombok提高编码效率-----不用写get set https://blog.csdn.net/v2sking/article/details/73431364
- poj 2135最小费用最大流
最小费用最大流问题是经济学和管理学中的一类典型问题.在一个网络中每段路径都有"容量"和"费用"两个限制的条件下,此类问题的研究试图寻找出:流量从A到B,如何选择 ...
- Ubuntu 16.04安装PDF阅读器FoxitReader
下载: https://www.foxitsoftware.cn/downloads/ 安装: tar zxvf FoxitReader2.4.1.0609_Server_x64_enu_Setup. ...
- Windows 10+Ubuntu 16.04在MBR分区上安装双系统(转)
以下内容转自这篇博客: http://www.cnblogs.com/Duane/p/5424218.html http://www.cnblogs.com/Duane/p/6776302.html( ...
- maven更改镜像路径为阿里镜像,以便下载速度快
1.maven更改镜像路径为阿里镜像,以便下载速度快 2.maven每更新一次镜像地址,都会重新下载一次包 3. 怎么配maven链接阿里云的镜像详细步骤 修改maven根目录下的conf文件夹中的s ...
- 架构师速成6.7-设计开发思路-uml
uml是什么东西?统一建模语言.一门语言.是用来进行软件设计的一门语言. 事实上一门语言的诞生并不伟大,让大多数人都使用才足够伟大. uml就是一门伟大的语言.由于眼下软件设计的唯一语言就是它. UM ...
- iOS开发——代码生成TabBar与视图切换具体解释
我在之前多篇博客中解说了在不使用storyboard而使用nib文件的情况下.使用代码生成导航栏并进行跳转,具体能够參考<iOS开发--界面跳转与返回及视图类型具体解释><iOS纯代 ...
- Python3基础(五) 函数
函数(function)是组织好的.可重复使用的.具有一定功能的代码段.函数能提高应用的模块性和代码的重复利用率,Python中已经提供了很多内建函数,比如print(),同时Python还允许用户自 ...