【发布时间】:2017-06-24 08:20:49
【问题描述】:
在我的 Zend Framework 2 应用程序的Factory 类中,我经常使用这样的构造:
// The signature was actually wrong, since it was always the `AbstractPluginManager` (or the `Zend\ServiceManager\ServiceManager` for the "common" services) and not just `ServiceLocator`, and it also was used as `AbstractPluginManager` (or `ServiceManager` for the "common" services). The `ServiceLocatorInterface` did not provide the `getServiceLocator()` method.
public function createService(ServiceLocatorInterface $serviceLocator)
{
// the common ServiceLocator
$realServiceLocator = $serviceLocator->getServiceLocator();
$myServiceFoo = $realServiceLocator->get('My\Service\Foo');
$myServiceBar = new \My\Service\Bar($myServiceFoo);
...
}
所以为了访问“通用”服务,我首先检索了ServiceLocator。对于Hydrators、Controllers 和其他拥有自己的ServiceManagers 的服务,这种方法是必要的。因为对于他们来说,输入 ServiceLocator 是 AbstractPluginManager 而不是 Zend\ServiceManager\ServiceManager。
现在我为我的工厂迈出了迁移的第一步,并替换了一些常见的东西:
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
// the common ServiceLocator
$realServiceLocator = $container->getServiceLocator();
$myServiceFoo = $realServiceLocator->get('My\Service\Foo');
$myServiceBar = new \My\Service\Bar($myServiceFoo);
...
}
$container->getServiceLocator()如何适配ZF3?
【问题讨论】:
-
ZF3 服务管理器的迁移指南值得一看,插件管理器部分解释了插件管理器是如何改变的,zendframework.github.io/zend-servicemanager/migration/… 在 AbstractPluginManager 部分(所有 zends 组件插件管理器继承的类) ) 它解释了构造函数现在接受父容器实例作为第一个参数;这通常是应用程序级别的 ServiceManager 实例”。对于 ZF 插件管理器,您在插件工厂中获得
$realServiceLocator作为$container
标签: zend-framework2 migration service-locator zend-framework3 zend-servicemanager