1

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?

automatix
  • 14,018
  • 26
  • 105
  • 230

1 Answers1

2

This error message looks like a Symfony error message, not a PHP-DI one.

I'm guessing that Symfony scans all your classes for autowiring, including your controller (even though it's not necessary).

This is the first time I hear of this, I guess you need to disable Symfony's autowiring entirely, or for some specific folders? If you can send a pull request to PHP-DI's documentation that would be awesome :)

Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261
  • Hello Matthieu! Thank you for your answer! Now it's working. There were two places I had to repair in my code: 1. Symfony: In the `services.yaml` at lease one of the following changed had to be made: a. set `services.autoconfigure` (not `service.autowire`) to `false` and/or b. add the path to the `Controller` directory to the `service.App\.exclude` list (or disable the `services.App` at all). 2. PHP-DI: I was using `DI\create(...)` and getting the error "[...] cannot be resolved: Parameter [...] has no value defined or guessable". This call had to be replaced by the `DI\get(...)`. – automatix Mar 23 '18 at 16:28
  • I'll send a pull request to PHP-DI's documentation later. – automatix Mar 23 '18 at 16:31