[英]ServiceManager in ZF3
I know that this has been covered extensively in other threads, but I'm struggling to work out how to replicate the effect of $this->getServiceLocator() from ZF2 controllers in ZF3 ones. 我知道这已经在其他线程中得到了广泛的介绍,但是我很难弄清楚如何从ZF3控件中的ZF2控制器复制$ this-> getServiceLocator()的效果。
I have tried creating a factory using the various other answers and tutorials that I've found here and elsewhere, but ended up in a mess with each of them, so I'm pasting my code as it was when I started in the hope that someone can point me in the right direction? 我尝试使用我在这里和其他地方找到的各种其他答案和教程来创建一个工厂,但最终却陷入了混乱,所以我粘贴了我的代码,就像我开始希望那样有人可以指出我正确的方向吗?
From /module/Application/config/module.config.php 来自/module/Application/config/module.config.php
'controllers' => [
'factories' => [
Controller\IndexController::class => InvokableFactory::class,
],
],
From /module/Application/src/Controller/IndexController.php 来自/module/Application/src/Controller/IndexController.php
public function __construct() {
$this->objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
$this->trust = new Trust;
}
You can not use $this->getServiceLocator() in controller any more . 你不能再在控制器中使用$ this-> getServiceLocator()了 。
You should add one more class IndexControllerFactory where you will get dependencies and inject it in IndexController 您应该再添加一个类IndexControllerFactory ,您将获得依赖项并将其注入IndexController
First refactor your config: 首先重构你的配置:
'controllers' => [
'factories' => [
Controller\IndexController::class => Controller\IndexControllerFactory::class,
],
],
Than create IndexControllerFactory.php 比创建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 refactor you IndexController to get dependencies 最后重构您的IndexController以获取依赖项
public function __construct(\Doctrine\ORM\EntityManager $object) {
$this->objectManager = $object;
$this->trust = new Trust;
}
You should check official documentation zend-servicemanager and play around a little bit... 你应该查看官方文档zend-servicemanager并玩一下......
Whilst the accepted answer is correct, I will implement mine a bit differently by injecting the container into the controller and then get other dependencies in 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, be sure to register the factory like so: 在你的module.config中,一定要像这样注册工厂:
'controllers' => [
'factories' => [
ControllerName::class => Controller\Factory\ControllerNameFactory::class,
],
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.