Closed ibrasho closed 10 years ago
This can't be the cause. Container::offsetGet
calls $this->make
, so it must go through Application::make
.
Yes, I've been investigating..
Turns out that $app->deferredProviders
doesn't get until all eager providers are registered. Which is why trying to load a deferred provider in the register()
method throws an exception.
Any possible solutions?
I'm trying to register a middleware, and I need the Monolog instance.
You shouldn't load any dependencies from the container from register
. You can of course do something like the following:
$this->app['key'] = $this->app->share(function($app)
{
return new Class($app['log']);
});
Now the container lookup is wrapped in a closure so not immediately executed.
I got around this by passing the class name of the middleware, instead of an instance.
This allows the loading to happen after deferred providers have been registered.
I'm not sure if this happened to anyone, but trying to use
$app['log']
for example in aregister()
method of aServiceProvider
with throw aReflectionException
.The reason is that
$app['log']
doesn't callApplication::loadDeferredProvider('log')
like$app->make('log')
does, it just delegate the call toContainer::offsetGet()
.A proposed solution is to make
Application
extendsArrayAccess
, and ensure thatApplication::offsetGet()
load deferred providers if not loaded already.Of course, one could use
App::make
in the end, but I prefer to deal with the container in a consistent manner.Any other ideas? I have a pull request ready if that's the accepted solution.