如何获得将对DBAdapter出的ServiceManager的

问题描述:

我尝试用下面的语句来获取databaseadapter:如何获得将对DBAdapter出的ServiceManager的

$dbAdapter = $this->getServiceLocator()->get('db'); 

我得到一个错误 getServiceLocator‘“由名的一个插件’未在插件管理器发现Zend \ Mvc \ Controller \ PluginManager“

虽然在我的module.php中搜索(这是正确的一个文件来看?)我认为这可能是一个理解问题。我阅读了Ralph Eggert的文章和zend文档。我知道我可以通过servicemanager获得任何配置信息。但是我发现的所有文档都是Zend2的。

所以在我module.php我看到这样的事情(片段):

public function getServiceConfig() 
{ 
    return [ 
      'factories' => [ 
        Model\ImportTable::class => function($container) { 
         $tableGateway = $container->get(Model\ImportTableGateway::class); 
         return new Model\ImportTable($tableGateway); 
        }, 
        Model\ImportTableGateway::class => function ($container) { 
         $dbAdapter = $container->get(AdapterInterface::class); 
         $resultSetPrototype = new ResultSet(); 
         $resultSetPrototype->setArrayObjectPrototype(new Model\Import()); 
         return new TableGateway('t_dcl', $dbAdapter, null, $resultSetPrototype); 
        }, 
        Model\DclimportTable::class => function($container) { 
         $tableGateway = $container->get(Model\DclimportTableGateway::class); 
         return new Model\DclimportTable($tableGateway); 
        }, 
        Model\DclimportTableGateway::class => function ($container) { 
         $dbAdapter = $container->get(AdapterInterface::class); 
         $resultSetPrototype = new ResultSet(); 
         $resultSetPrototype->setArrayObjectPrototype(new Model\Dclimport()); 
         return new TableGateway('t_dcl_import', $dbAdapter, null, $resultSetPrototype); 
        }, 

在那里,我看到一个变量$将对DBAdapter,但我怎么能得到这个变量?上面的错误可能是因为我现在使用ZEND3了?这个方法是否被弃用?我找不到任何迁移信息。

无论如何,有人可以向我解释,如何将这些密钥从module.php中取出,并在这种情况下在那里创建自己的工厂?我知道这是一个非常基本的问题,但我认为如果我不能做到这一点,它总会再次超过我。

控制器内的服务定位器在版本2.7中已弃用,并在版本3.0中删除。

要修复代码:

  • 找到您拨打getServiceLocator(所有情况下),并确定它们检索服务。
  • 更新您的控制器以通过构造函数接受这些服务。
  • 如果您还没有,请为您的控制器创建一个工厂类。
  • 在工厂中,拉出相应的服务并将它们传递给控制器​​的构造函数。

更多详细信息可在migration docs找到。