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?
Tracked down myself.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community