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根, 可以快速深入文档的有用工具, 服务容器绑定也包括在内

Facade Class Service Container Binding
App Illuminate\Foundation\Application app
Artisan Illuminate\Console\Application artisan
Auth Illuminate\Auth\AuthManager auth
Auth (Instance) Illuminate\Auth\Guard  
Blade Illuminate\View\Compilers\BladeCompiler blade.compiler
Bus Illuminate\Contracts\Bus\Dispatcher  
Cache Illuminate\Cache\Repository cache
Config Illuminate\Config\Repository config
Cookie Illuminate\Cookie\CookieJar cookie
Crypt Illuminate\Encryption\Encrypter encrypter
DB Illuminate\Database\DatabaseManager db
DB (Instance) Illuminate\Database\Connection  
Event Illuminate\Events\Dispatcher events
File Illuminate\Filesystem\Filesystem files
Hash Illuminate\Contracts\Hashing\Hasher hash
Input Illuminate\Http\Request request
Lang Illuminate\Translation\Translator translator
Log Illuminate\Log\Writer log
Mail Illuminate\Mail\Mailer mailer
Password Illuminate\Auth\Passwords\PasswordBroker auth.password
Queue Illuminate\Queue\QueueManager queue
Queue (Instance) Illuminate\Queue\QueueInterface  
Queue (Base Class) Illuminate\Queue\Queue  
Redirect Illuminate\Routing\Redirector redirect
Redis Illuminate\Redis\Database redis
Request Illuminate\Http\Request request
Response Illuminate\Contracts\Routing\ResponseFactory  
Route Illuminate\Routing\Router router
Schema Illuminate\Database\Schema\Blueprint  
Session Illuminate\Session\SessionManager session
Session (Instance) Illuminate\Session\Store  
Storage Illuminate\Contracts\Filesystem\Factory filesystem
URL Illuminate\Routing\UrlGenerator url
Validator Illuminate\Validation\Factory validator
Validator (Instance) Illuminate\Validation\Validator  
View Illuminate\View\Factory view
View (Instance) Illuminate\View\View  

Laravel5.1学习笔记i14 系统架构6 Facade的更多相关文章

  1. Laravel5.1学习笔记12 系统架构4 服务容器

    Service Container 介绍 绑定的用法  绑定实例到接口 上下文绑定 标签 解析 容器事件 #介绍 The Laravel service container is a powerful ...

  2. Laravel5.1学习笔记13 系统架构5 Contract

    Contract 简介 为什么要用 Contract? Contract 参考 如何使用 Contract 简介 Laravel 中的 Contract 是一组定义了框架核心服务的接口.例如,Illu ...

  3. Laravel5.1学习笔记11 系统架构3 服务提供者

    服务提供者 简介 写一个服务提供者 Register注册方法 Boot 方法 注册提供者 缓载提供者 简介 Service providers are the central place of all ...

  4. Laravel5.1学习笔记10 系统架构2 应用程序结构

    应用程序结构 简介 根目录 App 目录 为应用程序设置命名空间 简介 默认的 Laravel 应用程序结构是为了给无论构建大型还是小型应用程序都提供一个良好的开始.当然,你可以依照喜好自由地组织应用 ...

  5. Laravel5.1学习笔记9 系统架构1 请求生命周期 (待修)

    Request Lifecycle Introduction Lifecycle Overview Focus On Service Providers Introduction When using ...

  6. ODI学习笔记2--ODI产品架构

    ODI学习笔记2--ODI产品架构 ODI产品架构: ODI提供了以下几种管理工具:Designer 用于定义数据转换逻辑,这是最常用的开发工具,大部分的开发任务,包括data store的定义,in ...

  7. Symfony2 学习笔记之系统路由

    mfony2 学习笔记之系统路由   漂亮的URL绝对是一个严肃的web应用程序必须做到的,这种方式使index.php?article_id=57这类的丑陋URL被隐藏,由更受欢迎的像 /read/ ...

  8. Linux学习笔记-Linux系统简介

    Linux学习笔记-Linux系统简介 UNIX与Linux发展史 UNIX是父亲,Linux是儿子. UNIX发行版本 操作系统 公司 硬件平台 AIX IBM PowerPC HP-UX HP P ...

  9. 源码学习之Spring (系统架构简单解析)

    Spring Framework 系统架构总览图 Spring Framework的模块依赖关系图 Spring Framework各个模块功能说明 Spring核心模块 模块名称 主要功能 Spri ...

随机推荐

  1. pogresql基础学习笔记

    命令行工具:psql 可视化工具:pgAdmin 查看所有表: 命令行:\d sql:select * from pg_tables WHERE schemaname='public'; 查看表结构: ...

  2. ceph 简介

    Ceph 存储集群 数据的存储 伸缩性和高可用性 CRUSH 简介 集群运行图 高可用监视器 高可用性认证 智能程序支撑超大规模 动态集群管理 关于存储池 PG 映射到 OSD 计算 PG ID 互联 ...

  3. UVA 10692 Huge Mod

    Problem X Huge Mod Input: standard input Output: standard output Time Limit: 1 second The operator f ...

  4. QT .pro文件的学习收获

    1. 载pro文件预定义宏: CONFIG(debug,debug|release){ DEFINES+=__DEBUG__ }else{ DEFINES+=__RELEASE__ macx:DEST ...

  5. JavaMail发送邮件后再通过JavaMail接收格式问题

    复杂邮件发送问题 转载请标明出处!https://www.cnblogs.com/dream-saddle/p/10978113.html 关于 JavaMail 如何发送邮件这里就不赘述了,网上有很 ...

  6. 1154 能量项链 2006年NOIP全国联赛提高组 codevs

    1154 能量项链  2006年NOIP全国联赛提高组 codevs 题目描述 Description 在Mars星球上,每个Mars人都随身佩带着一串能量项链.在项链上有N颗能量珠.能量珠是一颗有头 ...

  7. scp: useful commands

    Examples Copy the file "foobar.txt" from a remote host to the local host $ scp your_userna ...

  8. Hibernate插入错误:GenericJDBCException: could not insert:

    数据库中一般不能建立user(表名为User)表,将User类改名,又一次建立映射,问题就能够解决 当然,还有还有一种情况.就是类中id类型错误.要设置为Integer型才干够设置自己主动增长,否则也 ...

  9. C#保留2位小数几种场景总结 游标遍历所有数据库循环执行修改数据库的sql命令 原生js轮盘抽奖实例分析(幸运大转盘抽奖) javascript中的typeof和类型判断

    C#保留2位小数几种场景总结   场景1: C#保留2位小数,.ToString("f2")确实可以,但是如果这个数字本来就小数点后面三位比如1.253,那么转化之后就会变成1.2 ...

  10. react 使用

    我的有道云笔记 React 事件: 1.不能使用 return false; 来阻止元素的默认行为.需要在方法的最前面使用 e.preventDefault() 来阻止元素的默认行为(例如:a 标签的 ...