Multiple services of type

Notice: This thread is very old.
Member | 57


I have class \App\Model\Files\File

class File extends Nette\Object

And I have class \App\Model\Files\XmlFile

class XmlFile extends File

Another service need to work with \App\Model\Files\File, so I did this:

class SystemState extends Nette\Object
	public function __construct(\App\Model\Files\File $stateFile) {
		$this->stateFile = $stateFile;

and in presenter:

	/** @var \App\Model\SystemState @inject */
	public $sysState;

- App\Model\Files\File
- App\Model\Files\XmlFile

Problem is, that Tracy is not happy:

Service '28_App_Model_SystemState': Multiple services of type App\Model\Files\File found: 24_App_Model_Files_XmlFile, 25_App_Model_Files_File

I'm little bit lost in DI, can comeone point me to right direction? THANKS!

Member | 27

it is because you have defined two services, that are (subclasses) of class App\Model\Files\File – App\Model\Files\File itself and App\Model\Files\XmlFile (which extends App\Model\Files\File), so DI container does not know, which one of them inject into SystemState constructor.

Solution would be turning off autowiring on one of these services (DI will use service that is autowired) or specifying name of service, you want to inject to SystemState service.

So in your config.neon will look like this:

    fileService: \App\Model\Files\File
    systemState: \App\Model\SystemState(@fileService)



Great! Thanks!!

6 years ago