I'm just getting my head round this whole DI thing, but something seems to be happening that I wonder shouldn't.
If I register a class in my AppServiceProvider, then inject it into a controller method, I can seemingly also App::make()
it for the second time:
// class
class Foo
{
public function __construct()
{
pr('ECHO!');
}
}
// app service provider
$this->app->singleton('Foo', function () {
return new Foo();
});
// controller
public function view(Foo $foo) // will call
{
\App::make('Foo'); // will call
\App::make('Foo'); // won't call
}
ECHO!
ECHO!
What's going on? Is the container not supposed to resolve this singleton only once?
it seems like you did not load the app service provider, make sure to add the service provider to the providers array in config\app.php.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community