【发布时间】:2013-03-20 12:47:49
【问题描述】:
我只是将我的 ZF2 更新到 V2.1.4,以便我可以使用 Zend\Form 类的 init 方法将 ServiceLocator 注入 FieldSet,正如 Zend documentation 所解释的那样。
我在 Global.php 和 Module.php 文件中有数据库驱动程序和服务的定义,还解释了documentation。该服务在所有项目中都可以正常工作。
这是我的字段集代码:
class UserFieldset extends Fieldset implements ServiceLocatorAwareInterface{
protected $serviceLocator;
public function setServiceLocator(ServiceLocatorInterface $sl) {
$this->serviceLocator = $sl;
}
public function getServiceLocator() {
return $this->serviceLocator;
}
public function init() {
var_dump($this->serviceLocator);
}
public function __construct() {
.........
}
}
我的问题如下:
在 UserFieldset::init() 方法内部,$this->serviceLocator 不是 Zend\ServiceManager\ServiceManager 的实例,就像从控制器获取服务时那样。 实例是 Zend\Form\FormElementManager 并使用 var_dump,看它没有我的 Zend 配置的任何服务。
如何使用 DI 在 Fieldset 中创建 Zend\ServiceManager\ServiceManager 实例?
请帮帮我。
【问题讨论】:
标签: dependency-injection zend-framework2 zend-form service-locator servicemanager