ServiceManager in ZF3

I know this was widespread in other threads, but I'm trying my best to replicate the effect of $ this-> getServiceLocator () from ZF2 controllers to ZF3.

I tried to create a factory using various other answers and training materials that I found here and elsewhere, but ended up encountering each of them, so I paste my code in the way it was when I started with what can someone point me in the right direction?

From / module / Application / config / module.config.php

'controllers' => [ 'factories' => [ Controller\IndexController::class => InvokableFactory::class, ], ], 

From / module / Application / src / Controller / IndexController.php

 public function __construct() { $this->objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager'); $this->trust = new Trust; } 
+6
source share
2 answers

You can no longer use $ this-> getServiceLocator () in the controller .

You must add another IndexControllerFactory class, where you get the dependencies and enter it in the IndexController

Reorganize your configuration first:

 'controllers' => [ 'factories' => [ Controller\IndexController::class => Controller\IndexControllerFactory::class, ], ], 

Create IndexControllerFactory.php

 <?php namespace ModuleName\Controller; use ModuleName\Controller\IndexController; use Interop\Container\ContainerInterface; use Zend\ServiceManager\Factory\FactoryInterface; class IndexControllerFactory implements FactoryInterface { public function __invoke(ContainerInterface $container,$requestedName, array $options = null) { return new IndexController( $container->get(\Doctrine\ORM\EntityManager::class) ); } } 

At the end of refactoring, you specify an IndexController to get the dependencies

 public function __construct(\Doctrine\ORM\EntityManager $object) { $this->objectManager = $object; $this->trust = new Trust; } 

You should check the official zend-servicemanager documentation and play a little ...

+13
source

Although the accepted answer is correct, I will implement mine a little differently, introducing the container into the controller, and then I will get other dependencies in the constructor, like so ...

 <?php namespace moduleName\Controller\Factory; use Interop\Container\ContainerInterface; use Zend\ServiceManager\Factory\FactoryInterface; use moduleName\Controller\ControllerName; class ControllerNameFactory implements FactoryInterface { public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { return new ControllerName($container); } } 

Your controller should look something like this:

 namespace ModuleName\Controller; use Doctrine\ORM\EntityManager; use Zend\ServiceManager\ServiceManager; class ControllerName extends \App\Controller\AbstractBaseController { private $orm; public function __construct(ServiceManager $container) { parent::__construct($container); $this->orm = $container->get(EntityManager::class); } 

In your module.config file, be sure to register the factory as follows:

 'controllers' => [ 'factories' => [ ControllerName::class => Controller\Factory\ControllerNameFactory::class, ], 
0
source

Source: https://habr.com/ru/post/1014884/


All Articles