If you want the auto-resolution to happen you need to bind using the fully qualified classname.
$app->singleton('Cviebrock\LaravelElasticsearch\LumenManager', function ($app) {
return new LumenManager($app, $app['elasticsearch.factory']);
});
I'm using Cviebrock\LaravelElasticsearch within Lumen 5.2
which I have registered in the AppServiceProvider:
$this->app->register(\Cviebrock\LaravelElasticsearch\LumenServiceProvider::class);
which registers:
$app->singleton('elasticsearch', function ($app) {
return new LumenManager($app, $app['elasticsearch.factory']);
});
In the route.php
I can (and it worked) if I do the following:
$elasticSearch = $app['elasticsearch'];
However inside the controller I must do this:
public function __construct()
{
$this->elasticsearch = app('elasticsearch');
}
If I try to type hint:
public function __construct(\Cviebrock\LaravelElasticsearch\LumenManager $es)
{
$this->elasticsearch = $es;
}
it squawks:
BindingResolutionException in Container.php line 839:
Unresolvable dependency resolving [Parameter #0 [ <required> $app ]] in class Cviebrock\LaravelElasticsearch\LumenManager
My goal is to try to make dependency injection happen for the controller instead of using app() call. Now I'm unsure if this is even possible but I thought it could happen so I can type hint and be able to inject but due to the error this went over my head and I need some help, understanding what I am doing wrong.
Thanks
You can always add a binding that pulls from the existing one.
$app->bind('Cviebrock\LaravelElasticsearch\LumenManager', function ($app) {
return $app['elasticsearch'];
});
Please or to participate in this conversation.