当前位置:网站首页>Understanding of service container, service provider and facade of laravel
Understanding of service container, service provider and facade of laravel
2022-06-22 04:57:00 【A Huang Junhui a】
Service container , There are some causal relationships between service providers and facade , To understand one alone , It's not easy to understand
Personal understanding , We create a new class or an interface to provide some kind of service , There are three objects , service ( That is, the newly created class or interface ), Service providers ( That is to say laravel Of ServiceProvider), Containers ( That is to say container app())
We go through Service providers , Put one service Sign up to In the container
Containers
That goes without saying laravel Of app It's a container , have access to app() To get the container object
service
Let's create a new service , In fact, it is a class or interface defined by itself ( But generally speaking, a service should be an interface )
We create a Animal Interface , And use Cat To achieve it , As a Service!
<?php
namespace App\Libs\MyLib;
interface Animal{
public function jump();
public function run();
}
<?php
namespace App\Libs\MyLib;
class Cat implements Animal{
public function jump()
{
dd("this is the Cat jump");
}
public function run()
{
dd("this is the Cat run");
}
}
There is a service on it , At this point, there must be a service provider ( Used to bind containers and services ( Also receive registration ))
Service providers
laravel Create a service provider Use php artisan make:provider XXXServiceProvider.php
After success , There will be one more in the directory Service provider classes 
We are register Zhongba Cat Bind to container 
above , The service is bound to the container through the service provider , We still can't use it at this time , We're still one step away , To register a service provider with laravel In the configuration file

Through the above operations , We bind the service to the container , And the framework will be configured accordingly at startup
So how do we use our registered services ?

The above ways , Service objects can be taken from the container
The above methods extract the service object Is it a little too long to write , Too annoying. , Do we have a simpler way to get objects , At this point, I will talk about Appearance
to glance at vendor/laravel/framework/src/iiiuminate/support/facades The files under the , They are all facade documents , We can Write your own template Facade class 

Similarly, we can register an alias for the facade 

Now we can use The facade uses static method calls to Method called , ( Be careful , Facade is only useful for methods , Calling properties has no effect )
边栏推荐
猜你喜欢

系统整理|这个模型开发前的重要步骤有多少童鞋忘记细心做好(实操)

厉害了!淮北两企业获准使用地理标志产品专用标志

网页设计与制作期末大作业报告——动画家宫崎骏

Slurm tutorial

LeetCode 437. Path sum III - binary tree series question 13

Solutions to MySQL 8.0 public key retrieval is not allowed errors

Daemon flow

Redis 主从复制

使用matplotlib实现GUI交互效果

Progress information collection for progress control of Zhiyuan project management SPM system
随机推荐
Go learning (II. Built in container)
基于深度学习的目标检测算法面试必备(RCNN~YOLOv5)
[sdx62] IPA log fetching instructions
In 2022, the postgraduate entrance examination ran aground | how to counter attack famous IT enterprises and become a programmer in three months
Debugging wechat built-in browser with chrome
Compilation des connaissances communes de la base de données numpy
LeetCode 437. Path sum III - binary tree series question 13
Segment tree & tree array template
商汤智慧医疗团队研究员解读智慧医疗下的器官图像处理
uwsgi-invalid-request-block-size invalid request block size: 21327 (max 4096)... Skip solution
The Impossible Triangle of NLP?
爬梯子&&卖卖股份的最佳时期(跑路人笔记)
Web page design and production final assignment report - College Students' online flower shop
yarn部署模式依赖预上传设置
[details] domestic website filing process and steps
Redis 主从复制
Software architecture and pattern: structure, component and relationship
【故障诊断】stitch.py脚本失效
Postman document parameterization
Common knowledge arrangement of numpy database