Laravel is able to automatically inject dependencies in controller constructs, etc. For example:
class Test {
public function __construct(Request $request) {}
}
App::make('Test');
The controller's constructor will receive the appropriate request facade.
Is there a way to do this with closures?
For example:
$closure = function(Request $input) {};
App::make($closure); // resolving the closure dependencies
No, it's not possible, you may read through the IoC container code here:
laravel/vendor/laravel/framework/src/Illuminate/Container/Container.php
on line466
As you see it tries to resolve and parse parents Class's
__constructor
method through reflections.I think that would be interesting to implement, as it's quite possible by extending the
Container
class to also support closures.I have made a couple of tests to make sure it is possible, so here they are:
Type hinting t5 will throw the exception.
This answers the question
As to how implement it, in my opinion you should have perfect knowledge of how reflections and Laravel IoC container work. I think this will not be implemented in near future as Laravel is basically built on classes. What are your use cases?