Service Container
Objective
In the previous section, we made a single entrance to some of the shared data, and the benefits were obvious, reducing unnecessary controller access. Let's take a look at service container in this section.
Description
Development environment: Windows 7
Laravel version: 5+
Ide:phpstorm
In the 25th quarter, we talked a lot, but we haven't covered the service container, let's talk about what this is.
Service Container
The so-called Service Container is a manager of a management class that relies on or relies on injection. What is "Dependency injection", first say "dependency", mention "dependency", that must be participants greater than or equal to 2 to form a "dependency" relationship, for the same class, a class may use another class, that is, the case of dependency; "Inject" is what ghost, English is "injected", or you Know "injection" , in the hospital, you have what disease, need what, give you what needle, lose what liquid, all depends on your specific illness illness, then give you "inject" these drugs. Then the combination of "dependency injection", that is, when you need this "dependency" relationship, when to "inject".
In programming, the method of dependency injection is implemented by means of a constructor function or setter.
Open routes.php, where all our routes are placed. Add a route to the file:
Route::get (' Bar ', function (bar $bar) {
A bit of programming experience of the students know that this route is not found bar class, because we have not defined this class, run will definitely error. (You don't have to experiment, you can test it yourself.) )
But we're adding a bar class to this route, which is different:
"Bar {#143}" is printed on my web page and should be $bar address.
But did you notice that we didn't explicitly give that closure method an instantiated object for the bar class, for example, as follows:
, but Laravel can find that class.
This is the function of service Container, which uses a core mechanism of PHP to "reflect" to find the class recursively, and then the service Container automatically binds (binding) the class.
Then there is automatic manual, if you want to manually bind (binding), you can also:
App::bind (' Bar ', function () {
After adding the above code, you have already bound the class by using the static method of the App class, bind (). Or at the beginning of that sentence, even if you are not bound, Laravel will help you bind.
Binding interface
When you thank an interface that needs to be implemented, you can bind the interface to the class on which the interface is to be implemented by binding. For example:
Interface Barinterface{}class Bar implements Barinterface{}route::get (' Bar ', function (Barinterface $bar) {
How can you let Service Container know your class Bar implementation barinterface? See below:
Interface Barinterface{}class Bar implements Barinterface{}app:bind (' Barinterface ', ' Bar '); Route::get (' Bar ', function (Barinterface $bar) {
Or:
Interface Barinterface{}class Bar implements Barinterface{}app:bind (' Barinterface ', ' Bar '); Route::get (' Bar ', function () { $bar = App::make ([' barinterface ']);
It's so simple.
Back to the beginning
At the time we were learning the route of Laravel, do not know you still have no impression, why the tutorial taught is:
And the first routes.php inside is the same type of child:
Route::get ('/', function () { return view (' Welcome ');
In fact, their functions are the same, but in order to embody the concept of MVC, their own duties, written in their own class, appear neat.
Summarize
Today's content is actually to tell us Laravel Service Container This container, its purpose and function. It is based on the reflection mechanism for recursive lookup classes, you can manually, or automatically, according to your own needs. More information can be found in the official documentation: Service Container.