当前位置:网站首页>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 !
边栏推荐
- CSDN Q & A_ Evaluation
- Troubleshooting and handling of an online problem caused by redis zadd
- View the port of the application published by was
- 告别996,IDEA中必装插件有哪些?
- C4D quick start tutorial - Chamfer
- 《统计学习方法》——第五章、决策树模型与学习(上)
- AMQ6126问题解决思路
- Matplotlib剑客行——容纳百川的艺术家教程
- Oracle delete tablespace and user
- Knowledge points are very detailed (code is annotated) number structure (C language) -- Chapter 3, stack and queue
猜你喜欢
![[staff] the lines and spaces of the staff (the nth line and the nth space in the staff | the plus N line and the plus N space on the staff | the plus N line and the plus N space below the staff | the](/img/dc/c0ea188ef353ded86759dbe9b29df3.jpg)
[staff] the lines and spaces of the staff (the nth line and the nth space in the staff | the plus N line and the plus N space on the staff | the plus N line and the plus N space below the staff | the

Redis installation and deployment (windows/linux)

Redis安装部署(Windows/Linux)

Microservice practice | declarative service invocation openfeign practice

WSL安装、美化、网络代理和远程开发

京东面试官问:LEFT JOIN关联表中用ON还是WHERE跟条件有什么区别

Chrome browser plug-in fatkun installation and introduction

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

微服务实战|手把手教你开发负载均衡组件

Chrome浏览器标签管理插件–OneTab
随机推荐
hystrix 实现请求合并
数构(C语言)——第四章、矩阵的压缩存储(下)
Cloud computing in my eyes - PAAS (platform as a service)
Don't look for it. All the necessary plug-ins for Chrome browser are here
以字节跳动内部 Data Catalog 架构升级为例聊业务系统的性能优化
Matplotlib swordsman Tour - an artist tutorial to accommodate all rivers
知识点很细(代码有注释)数构(C语言)——第三章、栈和队列
Cloudreve自建云盘实践,我说了没人能限制得了我的容量和速度
CSDN Q & A_ Evaluation
oracle删除表空间及用户
Microservice practice | Eureka registration center and cluster construction
微服务实战|声明式服务调用OpenFeign实践
AMQ6126问题解决思路
Actual combat of microservices | discovery and invocation of original ecosystem implementation services
Attributes of classfile
Complete solution of servlet: inheritance relationship, life cycle, container, request forwarding and redirection, etc
Knife4j 2.X版本文件上传无选择文件控件问题解决
The channel cannot be viewed when the queue manager is running
京东面试官问:LEFT JOIN关联表中用ON还是WHERE跟条件有什么区别
破茧|一文说透什么是真正的云原生