工厂被召唤但没有返回实例

时间:2014-02-18 06:34:55

标签: zend-framework2 servicemanager

我开始学习ZF-2.2但是ServiceManager有问题。

module.config.php

'service_manager' => array(
    'abstract_factories' => array(
        'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
        'Zend\Log\LoggerAbstractServiceFactory',
),

module.php

public function getServiceConfig(){
    return array(
        'invokables' => array(
            'PostDataMapper' => 'student\Model\PostDataMapper',
        ),
        'factories' => array(
            'PostIdentityMap' => function ($sm) {
        }),
    );
}

学生\ SRC \学生\模型\ PostDataMapper.php

    namespace student\Model;

    class PostDataMapper{



      private $dbcon="postgresql";
      protected $dbcon1 ="put";

    }

学生\ SRC \学生\模型\ PostIdendityMap.php

class PostIdentityMap {

    private $datamapper;

    public function __construct(PostDataMapper $pdm) {
        $this->datamapper = $pdm;  
    }
}

如果我这样尝试

$pim = $this->getServiceLocator()->get('PostIdentityMap');

---------------------------------------------

Zend\ServiceManager\Exception\ServiceNotCreatedException
C:\wamp\www\school\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:911

工厂被召唤但没有返回实例。

#0 C:\wamp\www\school\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php(1029): Zend\ServiceManager\ServiceManager->createServiceViaCallback(Object(Closure), 'postidentitymap', 'PostIdentityMap')
#1 C:\wamp\www\school\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php(601): Zend\ServiceManager\ServiceManager->createFromFactory('postidentitymap', 'PostIdentityMap')
#2 C:\wamp\www\school\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php(561): Zend\ServiceManager\ServiceManager->doCreate('PostIdentityMap', 'postidentitymap')
#3 C:\wamp\www\school\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php(503): Zend\ServiceManager\ServiceManager->create(Array)
#4 C:\wamp\www\school\module\student\src\student\Controller\BlogController.php(37): Zend\ServiceManager\ServiceManager->get('PostIdentityMap')
#5 C:\wamp\www\school\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractActionController.php(83): student\Controller\BlogController->postAction()
#6 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#7 C:\wamp\www\school\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#8 C:\wamp\www\school\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#9 C:\wamp\www\school\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractController.php(117): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#10 C:\wamp\www\school\vendor\zendframework\zendframework\library\Zend\Mvc\DispatchListener.php(114): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#11 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#12 C:\wamp\www\school\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#13 C:\wamp\www\school\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#14 C:\wamp\www\school\vendor\zendframework\zendframework\library\Zend\Mvc\Application.php(309): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#15 C:\wamp\www\school\public\index.php(17): Zend\Mvc\Application->run()
#16 {main}

1 个答案:

答案 0 :(得分:1)

当您使用闭包作为服务工厂时,您需要确保创建并返回所需的实例。

例如

public function getServiceConfig(){
    return array(
        'invokables' => array(
            'PostDataMapper' => 'student\Model\PostDataMapper',
        ),
        'factories' => array(
        'PostIdentityMap' => function ($sm) {
           return new Model\PostIdendityMap();
         }),
    );
}

考虑到对象没有构造函数;您可以将定义移动到invokables配置键。这意味着服务管理器将为您创建实例(无需工厂)。

public function getServiceConfig(){
    return array(
        'invokables' => array(
            'PostDataMapper'  => 'student\Model\PostDataMapper',
            'PostIdentityMap' => 'student\Model\PostIdendityMap',
        ),
    );
}

也;尝试遵循ZF2 skeleton application naming conventions - 'student'目录应该大写。