Support the ongoing development of Laravel.io →
posted 9 years ago
IOC

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?

Last updated 3 years ago.
0

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.

0

Thanks, i'll check that.

0

Sign in to participate in this thread!

PHPverse

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.