Laravel "Service container" and "service provider", which is the core of laravel?

Source: Internet
Author: User
Keywords Php laravel
In some materials, it is said that "service container" is the core of Laravel, "service provider" is also the core of Laravel.
Questions:
1, which is the core, or two of them together constitute the core?
2. What are their two relationships?
3. What are their two respective roles?
4. "Service container", "service provider", where is the specific "service"? For example, there is a pot filled with rice, a bowl of rice, where is the rice? Is it in the pot, in the bowl, or in the field?

Reply content:

In some materials, it is said that "service container" is the core of Laravel, "service provider" is also the core of Laravel.
Questions:
1, which is the core, or two of them together constitute the core?
2. What are their two relationships?
3. What are their two respective roles?
4. "Service container", "service provider", where is the specific "service"? For example, there is a pot filled with rice, a bowl of rice, where is the rice? Is it in the pot, in the bowl, or in the field?

You can go and see my note.
https://segmentfault.com/a/1190000004965752

Pick and choose

Contracts, ServiceContainer, serviceprovider, facades relations

    • Contracts contract, contract, that is, interface, define some rules, each implementation of this interface to implement the method inside

    • ServiceContainer implementation of contracts, concrete logic implementation

    • ServiceProvider ServiceContainer service provider, returns the instantiation of the ServiceContainer for use elsewhere, can be added to App/config provider, will be automatically registered in the container

    • Facades simplifies the way serviceprovider is called, and can call methods in ServiceContainer statically

Realize

Contracts interface can be written or not written, this does not define the
Define a servicecontainer to achieve the specific function

namespace App\Helper;class MyFoo{    public function add($a, $b)    {        return $a+$b;    }}

Define a serviceprovider for other places to use Servicecontain


  
   app->bind("myfoo", function(){            return new MyFoo();        });    }}

Add serviceprovider to the providers array in app/config.php to let the system register automatically

App\Providers\MyFooServiceProvider::class,

It can be used at this time, assuming that the controller uses

public function two($id=null){    //从系统容器中获取实例化对象    $myfoo = App::make("myfoo");    echo $myfoo->add(1,2);}

This is too cumbersome, you need to use make to get objects, for the sake of simplicity, you can use the façade function, define the façade Myfoofacade

namespace App\Facades;use Illuminate\Support\Facades\Facade;class MyFooFacade extends Facade{    protected static function getFacadeAccessor()    {        //这里返回的是ServiceProvider中注册时,定义的字符串        return 'myfoo';    }}

You can call it directly in the controller.

use App\Facades\MyFooFacade;public function two($id=null){    //从系统容器中获取实例化对象    $myfoo = App::make("myfoo");    echo $myfoo->add(1,2);    //使用门面    echo MyFooFacade::add(4,5);}

In general, a class has been customized so that service providers and facades can be used in order to make them easier to use elsewhere

Laravel in container is like a big bucket, is the core, every service is like filling a beverage bottles and jars, service provider as if connected to drink bottle and bucket of intubation, can not, directly to the Bucket pour also line, although this does not pay attention to. Want to go to a big bucket to drink drinks (a request came), facade is like Bailer help you from the bucket to scoop water, of course, do not also direct with the hand, although not sanitary, so facade can not.

Oh, yes, there is a contract, mainly for the good management of every service, in the carbonated water service, that also has the classification, Pepsi Pepsi is a contract tied together, snow bi mei year by another contract tied together, The main thing is to test good replacement.

In short, laravel these major concepts have done these things, nothing advanced.

If there is not comprehensive please leave a message.

About this recommendation a blog post
Laravel Learning Notes-the Magical service container

Simply put, the "service container" is the container for the service provided by the service provider, and the service container obtains the service through the service provider.

A laravel application is a "service container" in which the entire framework provides a variety of functions that are provided by the service provider and injected into the service container. In each "service Provider" register method bind , a state object is used to a variable, and that object is the object that actually serves the service.

The core of the service container and service provider is to reduce the RPS parameters of the query database

From https://segmentfault.com/q/1010000000602732

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.