ZF3 中的服务管理器
ServiceManager in ZF3
我知道这已在其他线程中广泛涉及,但我正在努力研究如何从 ZF3 控制器中的 ZF2 控制器复制 $this->getServiceLocator() 的效果。
我曾尝试使用我在此处和其他地方找到的各种其他答案和教程创建一个工厂,但最终都弄得一团糟,所以我将我的代码按原样粘贴开始希望有人能指出我正确的方向?
来自 /module/Application/config/module.config.php
'controllers' => [
'factories' => [
Controller\IndexController::class => InvokableFactory::class,
],
],
来自 /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。
你应该再添加一个 class IndexControllerFactory 你将在其中获取依赖项并将其注入 IndexController
首先重构您的配置:
'controllers' => [
'factories' => [
Controller\IndexController::class => Controller\IndexControllerFactory::class,
],
],
比创造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)
);
}
}
最后重构 IndexController 以获取依赖项
public function __construct(\Doctrine\ORM\EntityManager $object) {
$this->objectManager = $object;
$this->trust = new Trust;
}
您应该查看官方文档 zend-servicemanager 并尝试一下...
虽然接受的答案是正确的,但我将通过将容器注入控制器然后在构造函数中获取其他依赖项来实现我的有点不同...
<?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);
}
}
您的控制器应如下所示:
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);
}
在你的 module.config 中,一定要像这样注册工厂:
'controllers' => [
'factories' => [
ControllerName::class => Controller\Factory\ControllerNameFactory::class,
],
我知道这已在其他线程中广泛涉及,但我正在努力研究如何从 ZF3 控制器中的 ZF2 控制器复制 $this->getServiceLocator() 的效果。
我曾尝试使用我在此处和其他地方找到的各种其他答案和教程创建一个工厂,但最终都弄得一团糟,所以我将我的代码按原样粘贴开始希望有人能指出我正确的方向?
来自 /module/Application/config/module.config.php
'controllers' => [
'factories' => [
Controller\IndexController::class => InvokableFactory::class,
],
],
来自 /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。
你应该再添加一个 class IndexControllerFactory 你将在其中获取依赖项并将其注入 IndexController
首先重构您的配置:
'controllers' => [
'factories' => [
Controller\IndexController::class => Controller\IndexControllerFactory::class,
],
],
比创造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)
);
}
}
最后重构 IndexController 以获取依赖项
public function __construct(\Doctrine\ORM\EntityManager $object) {
$this->objectManager = $object;
$this->trust = new Trust;
}
您应该查看官方文档 zend-servicemanager 并尝试一下...
虽然接受的答案是正确的,但我将通过将容器注入控制器然后在构造函数中获取其他依赖项来实现我的有点不同...
<?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);
}
}
您的控制器应如下所示:
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);
}
在你的 module.config 中,一定要像这样注册工厂:
'controllers' => [
'factories' => [
ControllerName::class => Controller\Factory\ControllerNameFactory::class,
],