国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

When will Laravel put what I write into the service provider?
黃舟
黃舟 2017-05-16 16:47:48
0
3
454

For example, I created a new directory Services in the APP directory, and placed a service EmailService.php in it. When I want to call it in other controllers, I can directly quote:

use App\Services\EmailService;

This way you can use it normally.

Then the question is:
1. It can be used normally as above, so under what circumstances should this service be placed in the service providerGo inside? The relationship between
2, service provider, and service container has never been clear. Is service placed in service container#? ##Inside or Service Provider? Service Provider and Service ContainerWhat are their respective responsibilities?

黃舟
黃舟

人生最曼妙的風(fēng)景,竟是內(nèi)心的淡定與從容!

reply all(3)
習(xí)慣沉默

in服務(wù)提供者服務(wù)放進(jìn)服務(wù)容器.

An additional introduction to dependency injection containers: Learn to Stop Wiring and Love Laravel's Container

洪濤

The questioner will ask this question. I guess the questioner still doesn’t know much about dependency injection and laravel’s service container. (Of course, I don’t understand it very well. The following is just my simple understanding)

If you directly specify a class under a certain namespace inside your controller (this class should be an implementation of an interface (contract)), then this class will be coupled with the controller. When you want to modify this implementation class, you need to modify the controller and the methods called in the controller.

If you write it in the service provider, firstly, it can be decoupled (the parameters of the controller method are injected into the service implementation class through type hints), and secondly, if the service also depends on other services, laravel's service The container handles these dependencies automatically. Instead of using a bunch of use syntax in front of the controller to import these dependent libraries yourself. The third is to constrain the service through the interface to only provide the methods it should provide (single responsibility).

Basic understanding, please point out in the comment area if there are any mistakes.

我想大聲告訴你

Service container and service provider are two different things, without any mandatory relationship, so I will talk about them separately below.

Service container:
is a global associative array variable, which stores things in it, and then the object can be retrieved by name anywhere.

Service provider:
In each service provider, there is a piece of initialization code that will be used by the entire program, such as

Load the route definition when the application is initialized
Register a certain type of observer
Connect to the database and store the db instance in the service container

We can see from the above example that we always have to write these initialization codes. It's just that Laravel defines it as a specification, forcing you to classify and store the initialization code. (You can still put different types of initialization code all in app/Providers/AppServiceProvider.php)

Please correct me.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template