I have not understood laravel container very well. In my understanding, there are a lot of objects in the container that can be called. It can be used directly when needed, without the need for new. I would like to ask, do you all use this feature? How do I use this feature in projects? Is to split a service layer, and then put... I have not understood laravel container very well.
In my understanding, there are a lot of objects in the container that can be called.
It can be used directly when needed, without the need for new.
I would like to ask, do you all use this feature?
How do I use this feature in projects? Is a service layer separated and each service is put in the container?
Call in controller?
Or is it unnecessary?
Reply content:
I have not understood laravel container very well.
In my understanding, there are a lot of objects in the container that can be called.
It can be used directly when needed, without the need for new.
I would like to ask, do you all use this feature?
How do I use this feature in projects? Is a service layer separated and each service is put in the container?
Call in controller?
Or is it unnecessary?
The Laravel container is used to place services. these services are bound to the instance objects or closures in the container one by one. There are three binding methods: bind (), singleton () and instance (), parse the Service method from the Container: make (), these are in \ Illuminate \ Container, and \ Illuminate \ Foundation \ Application extends Container.
About how the Container works, when using Constructor Injection or Method Injection in the Controller, the Container is already in use, because the Container will automatically resolve these services for you, such:
Class AccountController extends Controller {// here is Method Injection. the Container will automatically parse the Request without needing to go to the new Request to obtain the object // The Container uses the Container to parse the service from the Container :: make () method public function test (Request $ request) {return $ request-> ip ();}}
Therefore, container services are always used. In general, containers are the places where service structures are separated. Traditionally, they are the services required to construct services within the dependent objects, at present, it is necessary to construct dependencies in the container location, which is automated and automatically injected into the dependent object. this achieves decoupling.
For the source code parsing of Container and Application, see:
Laravel learning notes: INER source code analysis
Laravel learning notes-IoC Container instantiation source code analysis