Support the ongoing development of Laravel.io →
IOC Packages Installation

In a fresh Laravel build, I cannot get overridden IoC bindings to work everywhere in the application.

Suppose a service provider that overrides a core class, e.g. cache:

class NewServiceProvider extends ServiceProvider
{
    protected $defer = true;

    public function register()
    {
        $this->app->bind('cache', function($app) {
        	return new \stdClass; // demo purpose
        });
    }

    public function provides()
    {
    	return ['cache'];
    }
}

The provider is then added at the bottom of app.providers config.

Now modify routes.php to the following and go check the result:

Route::get('/', function () {
    dd(app('cache'));
});

// Results in an empty stdClass being shown. It works!

However, fire up artisan tinker and do the same:

$ php artisan tinker
>>> app('cache')
=> Illuminate\Cache\CacheManager

Suddenly the override isn't working anymore...

The same behavior is encountered when processing event listeners...

Is this normal behavior and am I overlooking something? Or is this some kind of bug?

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

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.