当前位置:网站首页>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 )
 Insert picture description here
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
 Insert picture description here
After success , There will be one more in the directory Service provider classes
 Insert picture description here
We are register Zhongba Cat Bind to container
 Insert picture description here


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

 Insert picture description here
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 ?

 Insert picture description here
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
 Insert picture description here

 Insert picture description here
Similarly, we can register an alias for the facade
 Insert picture description here
 Insert picture description here

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 )

原网站

版权声明
本文为[A Huang Junhui a]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220452010699.html