You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Based on your example of the register method of the service provider, we need to explicitly define dependencies/arguments to add the class to the container.
publicfunctionprovides(string$id): bool
{
$services = [
Some\Controller::class, // class with Request and Model as dependencies.
];
returnin_array($id, $services);
}
publicfunctionregister(): void
{
$this->getContainer()->add('key', 'value');
$this->getContainer()
->add(Some\Controller::class)
->addArgument(Some\Request::class)
->addArgument(Some\Model::class)
}
I was wondering if it is possible to just add the class provided in the services array and let the container resolve/auto-wire. Namely, to be able to just do the following:
Fatal error: Uncaught ArgumentCountError: Too few arguments to function \Container\ServiceProvider\TestService::__construct(), 0 passed and exactly 1 expected
The text was updated successfully, but these errors were encountered:
creative-andrew
changed the title
Question: Autowiring inside the service provider.
Question: How to achieve autowiring inside the service provider?
Jan 22, 2023
By design, autowiring is only supposed to work completely separately to definitions, if a service/dependency has a definition of any kind, it won't attempt any type of autowiring, this is because historically, watching people use the container, it became clear that it was possible for "magic" to be occurring and for people not to realise how it was working.
Hello ✋ ,
Based on your example of the
register
method of the service provider, we need to explicitly define dependencies/arguments to add the class to the container.I was wondering if it is possible to just add the class provided in the services array and let the container resolve/auto-wire. Namely, to be able to just do the following:
I am trying to make the container call a method
init
on all the services defined within the service provider. For example:Container:
ServiceProvider
TestService:
The text was updated successfully, but these errors were encountered: