1

请注意:这是ZF测试1.我怀疑这是当前版本ZF2依赖注入 - 注入表到类

一样,我只是试图依赖注入我的一些课程。我以为我有这个问题,因为我已经用很多东西来管理它,比如将database configurations,DB adaptersauth adapters注入到我的UserMapper课程中。

我试图做一些与我的一些其他类,并根本无法工作为什么它只是不像我的UserMapper类一样工作。现在我看到UserMapper类正被注入到一个控制器中,该控制器在配置文件的顶部为其设置了一个别名。

所以我猜没有通过使用控制器首先注入...我应该如何注入我的模型?我使用的是标准ZF2 Skeleton by EvanDotPro

我的模型是:

Application\Model\ 
Application\Model\DbTable 

我的配置目前看起来是这样:

<?php 
return array(
'bootstrap_class' => 'Application\Bootstrap', 
'layout'   => 'layouts/layout.phtml', 
'di'    => array(
    'instance' => array(
     'alias' => array(
      'index'    => 'Application\Controller\IndexController', 
      'view'    => 'Zend\View\PhpRenderer' 
     ), 

     'Zend\View\HelperLoader' => array(
      'parameters' => array(
       'map' => array(
        'url' => 'Application\View\Helper\Url', 
       ), 
      ), 
     ), 

     'Zend\View\HelperBroker' => array(
      'parameters' => array(
       'loader' => 'Zend\View\HelperLoader', 
      ), 
     ), 

     'Application\Controller\IndexController' => array(
      'parameters' => array(
       'userMapper' => 'Application\Model\UserMapper', 
       'flashMessenger' => 'Zend\Mvc\Controller\Plugin\FlashMessenger' 
      ) 
     ), 

     'Application\Model\UserMapper' => array(
      'parameters' => array(
       'db' => 'Zend\Db\Adapter\PdoMysql', 
       'authAdapter' => 'Zend\Authentication\Adapter\DbTable', 
       'userTable' => 'Application\Model\DbTable\UserTable' 
      ) 
     ), 

     'Application\Model\DbTable\UserTable' => array(
      'parameters' => array(
       'config' => 'Zend\Db\Adapter\PdoMysql', 
      ) 
     ), 

      // Auth adapter 

     'Zend\Authentication\Adapter\DbTable' => array(
      'parameters' => array(
       'zendDb' => 'Zend\Db\Adapter\PdoMysql', 
       'tableName' => 'users', 
       'identityColumn' => 'username', 
       'credentialColumn' => 'password', 
       'credentialTreatment' => 'MD5(CONCAT(?,"OSalTyr$"))' 
      ) 
     ), 

     // DB Adapter 

     'Zend\Db\Adapter\PdoMysql' => array(
      'parameters' => array(
       'config' => array(
        'host' => 'localhost', 
        'username' => 'root', 
        'password' => '', 
        'dbname' => 'blahblah', 
       ), 
      ), 
     ), 

     // View 

     'Zend\View\PhpRenderer' => array(
      'parameters' => array(
       'resolver' => 'Zend\View\TemplatePathStack', 
       'options' => array(
        'script_paths' => array(
         'application' => __DIR__ . '/../views', 
        ), 
       ), 
       'broker' => 'Zend\View\HelperBroker', 
      ), 
     ), 

    ), 
), 

'routes' => array(
    'default' => array(
     'type' => 'Zend\Mvc\Router\Http\Regex', 
     'options' => array(
      'regex' => '/(?P<controller>[^/]+)(/(?P<action>[^/]+)?)?', 
      'spec'  => '/%controller%/%action%', 
      'defaults' => array(
       'controller' => 'error', 
       'action'  => 'index', 
      ), 
     ), 
    ), 
    'home' => array(
     'type' => 'Zend\Mvc\Router\Http\Literal', 
     'options' => array(
      'route' => '/', 
      'defaults' => array(
       'controller' => 'index', 
       'action'  => 'index', 
      ), 
     ), 
    ), 
), 
); 

感谢,多米尼克

回答

0

AHAH ...我发现为什么这不起作用。

虽然上面的配置是正确的,我正在定义我想要注入什么......我实际上并没有使用注入的有问题的对象实例。

我想在使用'new thingy();'该DI会奇迹般地拿起它有依赖关系并在需要时应用它们,但这种情况并非如此......

我INFACT需要做的......是注入thingy类到我UserMapper类像一个链和然后使用注入的实例,而不是创建它的实例,而不注入其中的东西。

像:

RegisterController需要UserMapper所以它注入并使用该实例 UserMapper需要Thingy所以在注入并使用该实例