When is laravel container used?

Source: Internet
Author: User
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

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.