当前位置:网站首页>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 !
边栏推荐
- Don't look for it. All the necessary plug-ins for Chrome browser are here
- Oracle delete tablespace and user
- Win10 uses docker to pull the redis image and reports an error read only file system: unknown
- JVM instruction mnemonic
- Watermelon book -- Chapter 5 neural network
- Matplotlib剑客行——没有工具用代码也能画图的造型师
- Long summary (code with comments) number structure (C language) -- Chapter 4, string (Part 1)
- oracle修改数据库字符集
- Chrome video download Plug-in – video downloader for Chrome
- 微服务实战|原生态实现服务的发现与调用
猜你喜欢
Flink - use the streaming batch API to count the number of words
WSL安装、美化、网络代理和远程开发
Avoid breaking changes caused by modifying constructor input parameters
【Go实战基础】如何安装和使用 gin
[go practical basis] how can gin get the request parameters of get and post
Microservice practice | fuse hytrix initial experience
「Redis源码系列」关于源码阅读的学习与思考
[staff] common symbols of staff (Hualian clef | treble clef | bass clef | rest | bar line)
数构(C语言--代码有注释)——第二章、线性表(更新版)
Data type case of machine learning -- using data to distinguish men and women based on Naive Bayesian method
随机推荐
Programmers with ten years of development experience tell you, what core competitiveness do you lack?
AMQ6126问题解决思路
Chrome user script manager tempermonkey monkey
远程连接IBM MQ报错AMQ4036解决方法
一篇详解带你再次重现《统计学习方法》——第二章、感知机模型
Chrome视频下载插件–Video Downloader for Chrome
西瓜书--第六章.支持向量机(SVM)
Matplotlib剑客行——布局指南与多图实现(更新)
Microservice practice | Eureka registration center and cluster construction
VIM操作命令大全
微服务实战|手把手教你开发负载均衡组件
Oracle modify database character set
Microservice practice | declarative service invocation openfeign practice
我服了,MySQL表500W行,居然有人不做分区?
Chrome browser tag management plug-in – onetab
Shengshihaotong and Guoao (Shenzhen) new energy Co., Ltd. build the charging pile industry chain
自定义Redis连接池
Attributes of classfile
MySql报错:unblock with mysqladmin flush-hosts
队列管理器running状态下无法查看通道