The IoC Container control reversal ContainerBasic Binding Foundation Bindings
Now the we ' ve learned about dependency injection, let's explore inversion of control containers.
IoC containers make managing your class dependencies much + convenient, and Laravel ships with a very powerful containe R. The IoC container is the certral piece of the Laravel framework, and it's what allows all of the framework ' s Jcomponen TS jto work together. In fact, the Laravel application class extends the Container class!
We've learned about dependency injection, so let's explore the "Control reversal Container" (IoC) together. IOC containers can make it easier for you to manage dependency injection, and the Laravel framework has a very powerful IOC container. At the heart of the Laravel is the IOC container, which allows the framework to work together well with each component. In fact Laravel's application class is inherited from Container Class!
IoC Container Control Reversal Container
Inversion of control containers make dependency injection more convenient. How to resolve a given class or interface >is defined once in the container, which manages resolving and injecting thos e objects throughout your application.
Controlling reversal containers makes dependency injection more convenient. When a class or interface is defined in a container, how do you handle it-how do you manage and inject these objects into your application?
In a Laravel application, the IoC container can be accessed via the App facade. The container has a variety of methods, but we'll start with the most basic. Let's continue to use we billerinterface and Billingnotifierinterface from the previous chapter, and assume this our APPL Ication is using the Stripe to process payments. We can bind the Stripe implementation of the interface to the container as this:
In the Laravel app, you can access the control reversal container via the app. Containers have many methods, but we start with the basics. Let's continue using the Billerinterface and billingnotifierinterface written in the previous chapter, and suppose we use stripe for payment operations. We can bind the stripe payment implementation to the container, like this:
App::bind (' Billerinterface ', function () { return new Stripebiller (App::make (' billingnotifierinterface '));});
Notice that within our Billinginterface resolver, we also resolve a billingnotifierinterface implementation. Let's define that binding as well:
Note that when we deal with billinginterface, we need an additional billingnotifierinterface implementation, that is, another bind:
App::bind (' Billingnotifierinterface ', function () { return new Emailbillingnotifier;});
So, as you can see, the container are a place to store Closures that resolve various classes. Once A class have been registered with the container, we can easily resolve it from anywhere in our application. We can even resolve other container bindings within a resolver.
As you can see, this container is a place to store a variety of bindings (the translator notes: This is a simple point to understand.) Then pull the anonymous function, the closure is pulled away. )。 Once a class is bound in a container, we can easily invoke it anywhere in the application. We can even write another bind within the bind function.
Have Acne?
The Laravel IOC container is a drop-in replacement for the pimple IOC container by Fabien Potencier. So, if you ' re already using pimple on a project, feel free to upgrade to the illuminate Container component for a few more features!
The illuminate container of the laravel framework and another IOC container named Pimple are replaceable. So if you've used pimple before, you can boldly upgrade to illuminate Container, which has more new features!
Once we ' re using the container, we can switch interface implementations with a single line change.
For example, consider the following:
Once we have used the container, the implementation of the switching interface is a matter of one line of code.
Consider the following code, for example:
Class Usercontroller extends basecontroller{public function __construct (billerinterface $biller) { $ This->biller = $biller; }}
When this controller is instantiated via the IoC container, the Stripebiller, which includes the Emailbillingnotifier, wil L is injected into the instance. Now, if we are want to notifier implementation, we can simply change the binding to this:
When the controller is instantiated by the container, the stripebiller containing the emailbillingnotifier will be injected into the controller (translator Note: See the two bind above). If we want to change the presentation now, we can simply change the code to this:
App::bind (' Billingnotifierinterface ', function () { return new Smsbillingnotifier;});
Now, it doesn ' t matter where the notifier are resolved in our application, we'll now always get the Smsbillingnotifier im Plementation. Utilizing this architecture, our application can is rapidly shifted to new implementations of various services.
Now, no matter where the application needs a prompt, we always get the Smsbillingnotifier object. With this structure, our applications can switch quickly between different implementations.
Being able to the change implementations of an interface with a single line is amazingly powerful. For example, imagine we want to the change of our SMS service from a legacy provider to Twilio. We can develop a new Twilio implementation of the notifier and swap our binding. If we have problems and the transition to Twilio, we can quickly change back to the legacy provider by making a single Io C binding change. As can see, the benefits of using dependency injection go beyond what is immediately obvious. Can you think the more benefits for using dependency injection and an IoC container?
It's a powerful ability to switch code implementations just by changing one line. For example, we want to replace the SMS service with Twilio from the original provider. We can develop a new Twilio class (Translator note: Of course inherit from Billingnotifierinterface) and then modify the binding statement. If Twilio has any mistakes, we can quickly switch back to the original SMS provider by simply modifying one line of code. See, the benefits of dependency injection are many. Can you come up with a few more benefits of using dependency injection and control reversal containers?
Sometimes wish to resolve only one instance of a given class throughout your entire application. This can is achieved via the Singjleton method on the container class:
Want to instantiate only one class at a time in your app? No problem, use the Singleton method:
App::singleton (' Billingnotifierinterface ', function () { return new Smsbillingnotifier;});
Now, once the container have resolved the billing notifier once, it'll continue to use that same instance for all subsequ ENT requests for that interface.
As soon as this container generates this Cue object once, the container will only provide the same object in the next build request.
The instance method on the container was similar to singleton, however, and you were able to pass an already existing object ins Tance. The instance give to the container would be a used each time the container needs a instance of that class:
The instance method of a container is similar to the Singleton method, except that instance can bind an already existing object. The container then returns the object each time.
$notifier = new Smsbillingnotifier; App::instance (' Billingnotifierinterface ', $notifier);
Now the we ' re familiar with the basic container resolution using Closures, let's dig into it most powerful feature:the Abil ity to resolve class via reflection.
Now that we are familiar with the basic usage of the container, let's dig deeper into its more powerful features: relying on reflection to process classes and interfaces.
Stand Alone Container container operates independently
Working on a project this isn ' t built on Laravel? You can still utilize Laravel's IoC container by installing the Illuminate/container package via composer!
Your project is not using Laravel? But you can still use Laravel's IOC container! Just use composer to install the Illuminate/container bag.
From apprentice to Artisan translation 03