zend-framework2servicemanager

The factory was called but did not return an instance


I started learning ZF-2.2 but have a problem with 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) {
        }),
    );
}

student\src\student\Model\PostDataMapper.php

    namespace student\Model;

    class PostDataMapper{



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

    }

student\src\student\Model\PostIdendityMap.php

class PostIdentityMap {

    private $datamapper;

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

If i tried like this

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

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

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

The factory was called but did not return an instance.

#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}

Solution

  • When you use a closure as a service factory you will need to ensure you create and return the instance you require.

    For example

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

    Considering the object has no constructor; You could move the definition to the invokables configuration key. This means the service manager will create the instance for you (without the need for a factory).

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

    Also; Try to follow the ZF2 skeleton application naming conventions - The 'student' directory should be capitalized.