D
D
Dmitry2017-07-20 22:34:16
Zend Framework
Dmitry, 2017-07-20 22:34:16

How to properly use DI container with constructor injection in ZF2?

How to properly inject a dependency through an interface through a class constructor?
In Module::getServiceConfig() I described the factory through a class that implements the Zend\ServiceManager\FactoryInterface interface:

'factories' => [
    'MyModule\Model\RelatedLists\ServiceInterface' => 'MyModule\Model\RelatedLists\Factory\ServiceFactory',
]

This option will work if you only create a service through the ServiceLocator.
What should I do to be able to inject this service through its interface in a class constructor, for example, a controller?
public function __construct(\MyModule\Model\RelatedLists\ServiceInterface $service) {...}

How to competently organize dependency resolving, for example, as in Laravel, where for these purposes you can select a ServiceProvider and bind (bind/singleton, for example) the interface to the implementation (abstract => concrete)?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question