3

Coming for a JavaScript and Java background, I find dependency injection in Laravel (or PHP) extremely confusing.

For example (Laravel 5.1), in Middleware/Authenticate the constructor takes a Guard object. I have search the codebase and the class Authenticate is never instantiated.

So where is the Guard coming from then?

Thanks, SK.

2

2 Answers 2

1

Look at the laravel documentation:

Dependency injection is a fancy phrase that essentially means this: class dependencies are "injected" into the class via the constructor or, in some cases, "setter" methods. http://laravel.com/docs/5.1/container

You can instantiate Guard in your code or fetch instance of Guard from somewere and pass it to Middleware/Authenticate constructor.

EDITED : Guard is injected into the Middleware/Authenticate automatically somewere in laravel framework code at some point. You better take a look on different example of Dependency injection in Laravel where all is visibly in your code.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes I have read that and I have used 'dependency injection' in JavaScript. My question is how Guard is injected into the Middleware/Authenticate automatically.
Where is being DI in Laravel, I mean creation of instance class of injectable object?
0

Defn : Dependency injection is a technique whereby one object supplies the dependencies of another object.

You could visit the link below for a detailed explanation with ioc container explanation Visit : https://www.thelearninguy.com/dependency-injection-in-laravel-using-ioc-container

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.