当前位置:网站首页>What is the function of laravel facade
What is the function of laravel facade
2022-07-02 09:26:00 【Yisu cloud】
laravel What is the function of facade
This article mainly shows you “laravel What is the function of facade ”, The content is simple and easy to understand , Clarity of organization , I hope I can help you solve your doubts , Now let Xiaobian lead you to study and learn “laravel What is the function of facade ” This is the article .
stay laravel in , The facade Facades For applications IoC The class of the service container provides a static interface ,Laravel As the static proxy of the underlying class in the service container , Compared to traditional static methods , It can provide syntax that is easier to test when maintaining .

The operating environment of this tutorial :windows10 System 、Laravel6 edition ,DELL G3 The computer .
laravel What's the use of facade
Introduce
Facades For application IoC Service container The class of provides a static interface .Laravel There are some in it Facades, Such as Cache etc. .Laravel As the facade of the underlying class in the service container “ Static proxy ”, Compared to traditional static methods , It can provide easier testing during maintenance 、 More flexible 、 Concise and elegant grammar .
explain
stay Laravel In this context , One Facade It's a class , Using this class, you can access an object from the container , This function is in Facade class .Laravel Of Facades And anything you define Facades, Will inherit Facade This class .
Yours Facade Class only needs to implement one method :getFacadeAccessor. In a container resolve What come out , It's all done in this way .Facade This base class uses __callStatic() Magic methods , It can be postponed to resolved On the object , come from Facade Call to .
therefore , When you use Facade When called , Like this :Cache:get,laravel From Ioc Service container Inside resolves Cache management class , Then call the above of this class get Method .Laravel Of Facades You can go to location services , It's a use Laravel Of Ioc Service container More convenient grammar .
advantage
Facade There are many advantages , It provides a simple 、 Easy to remember grammar , Let's use it without remembering the long class name Laravel Features provided , Besides , Because of their concern for PHP Unique usage of dynamic methods , Make them easy to test .
The actual use
The following example , I called Laravel Cache system of . Take a look at the following line of code first , You might think , This is a direct call Cache The one above this class is called get Static method of .
$value = Cache::get('key');however , If you check Illuminate\Support\Facades\Cache This class , You will find that there is no get This static method :
class Cache extends Facade {
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() { return 'cache'; }
}Cache This class inherits Facade The base class , It defines a name getFacadeAccessor() Methods . Be careful , What this method does is to return a Ioc Binding name , Here is the cache.
When the user is quoting anything in Cache This Facade When using static methods on ,Laravel It will start from Ioc Service container Go inside resolves cache This binding , And will execute the requested method on the object ( Here is the get This method ).
therefore , We're calling Cache::get When , What it really means is this :
$value = $app->make('cache')->get('key');Import Facades
Be careful , In the use of facade When , If a namespace is used in the controller , You need to put Facade Class is imported into this namespace . be-all Facades All under the global namespace :
<?php namespace App\Http\Controllers;
use Cache;
class PhotosController extends Controller {
/**
* Get all of the application photos.
*
* @return Response
*/
public function index()
{
$photos = Cache::get('photos');
//
}
}establish Facades
establish Facade Only three things are needed :
One IoC binding .
One Facade class .
One Facade Alias configuration .
Below we define a class :PaymentGateway\Payment .
namespace PaymentGateway;
class Payment {
public function process()
{
//
}
}We need to be able to Ioc Service container Go inside resolve This class . therefore , First, add a Service Provider binding :
App::bind('payment', function()
{
return new \PaymentGateway\Payment;
});The best way to register this binding is to create a new Service Provider , Name it PaymentServiceProvider , Then bind it to register On the way . Then configure laravel stay config/app.php This configuration file loads your Service Provider.
The next step is to create your own Facade class :
use Illuminate\Support\Facades\Facade;
class Payment extends Facade {
protected static function getFacadeAccessor() {
return 'payment';
}
}Last , If you will , You can go to Facade Add an alias , Put it in config/app.php In the configuration file aliases In the array .
You can call Payment Class process This method . like this :
Payment::process();
Above is “laravel What is the function of facade ” All the content of this article , Thank you for reading ! I believe we all have a certain understanding , I hope the content shared will be helpful to you , If you want to learn more , Welcome to the Yisu cloud industry information channel !
边栏推荐
- 分布式锁的这三种实现方式,如何在效率和正确性之间选择?
- 「Redis源码系列」关于源码阅读的学习与思考
- Sentinel reports failed to fetch metric connection timeout and connection rejection
- ClassFile - Attributes - Code
- 2022/2/13 summary
- [go practical basis] how to set the route in gin
- Dix ans d'expérience dans le développement de programmeurs vous disent quelles compétences de base vous manquez encore?
- Redis installation and deployment (windows/linux)
- Matplotlib swordsman Tour - an artist tutorial to accommodate all rivers
- Pool de connexion redis personnalisé
猜你喜欢

Matplotlib swordsman - a stylist who can draw without tools and code

Chrome user script manager tempermonkey monkey

Redis 序列化 GenericJackson2JsonRedisSerializer和Jackson2JsonRedisSerializer的区别

微服务实战|声明式服务调用OpenFeign实践

知识点很细(代码有注释)数构(C语言)——第三章、栈和队列

I've taken it. MySQL table 500W rows, but someone doesn't partition it?

盘点典型错误之TypeError: X() got multiple values for argument ‘Y‘

In depth analysis of how the JVM executes Hello World

自定義Redis連接池

机器学习之数据类型案例——基于朴素贝叶斯法,用数据辩男女
随机推荐
数构(C语言--代码有注释)——第二章、线性表(更新版)
【Go实战基础】gin 如何绑定与使用 url 参数
Pdf document of distributed service architecture: principle + Design + practice, (collect and see again)
Attributes of classfile
Chrome浏览器插件-Fatkun安装和介绍
Chrome user script manager tempermonkey monkey
JVM instruction mnemonic
京东高级工程师开发十年,编写出:“亿级流量网站架构核心技术”
Win10 uses docker to pull the redis image and reports an error read only file system: unknown
What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?
《统计学习方法》——第五章、决策树模型与学习(上)
Statistical learning methods - Chapter 5, decision tree model and learning (Part 1)
数构(C语言)——第四章、矩阵的压缩存储(下)
In depth analysis of how the JVM executes Hello World
WSL installation, beautification, network agent and remote development
AMQ 4043 solution for errors when using IBM MQ remote connection
Taking the upgrade of ByteDance internal data catalog architecture as an example, talk about the performance optimization of business system
Watermelon book -- Chapter 6 Support vector machine (SVM)
[go practical basis] how to set the route in gin
Redis zadd导致的一次线上问题排查和处理