I want to use my recently set up Symfony 4 project with PHP-DI 6 and PHP-DI Symfony Bridge 3.
My project structure looks like this:
|-config
|---dependencies
|-----common.php
...
|-src
...
|-Interop
|---Api
|---Website
|-----Controller
|-------IndexController.php
...
|---Services
|-----Dummy
|-------FooService.php
|-------FooServiceInterface.php
...
|-Kernel.php
The classes FooService
and BuzService
implement the FooServiceInterface
.
/config/dependencies/common.php
return [
IndexController::class => DI\create(IndexController::class),
FooServiceInterface::class => DI\create(FooService::class),
];
The IndexController
gets an instance of the FooServiceInterface
injected.
public function __construct(FooServiceInterface $fooService)
{
$this->fooService = $fooService;
}
The Kernel
extends the DI\Bridge\Symfony\Kernel
and implements its buildPHPDIContainer(...)
:
protected function buildPHPDIContainer(PhpDiContainerBuilder $builder)
{
$builder->addDefinitions(__DIR__ . '/../config/dependencies/common.php');
return $builder->build();
}
Everythings seems to be set up according to the PHP-DI Symfony Bridge documentation.
Everything works fine, if there is only one implementation of the FooServiceInterface
. But when I add a further one, e.g.:
class BuzService implements FooServiceInterface
I get an error:
RuntimeException
Cannot autowire service "App\Interop\Website\Controller\IndexController": argument "$fooService" of method "__construct()" references interface "App\Services\Dummy\FooServiceInterface" but no such service exists. You should maybe alias this interface to one of these existing services: "App\Services\Dummy\BuzService", "App\Services\Dummy\FooService". Did you create a class that implements this interface?
Why am I getting this error & how to get this structure working correctly?