错误:Zend \ ServiceManager \ Exception \ ServiceNotCreatedException

时间:2016-11-15 11:56:46

标签: php zend-framework2

我尝试创建一个模块用户我从zend 2.4的Blog Exemple中获得灵感,我构建了一个工厂来实例化我的控制器UsersController,但是我收到了这个错误:

An exception was raised while creating "Users\Controller\Users"; no instance returned

实际上,当我将服务带入控制器时会出现问题

class UsersController extends AbstractActionController {

    protected $userService;

    public function __construct(UserServiceInterface $userService){
        $this->userService = $userService ;
    }
}

然后我定义了UserServiceInterface并创建了一个' Users \ Controller \ UserController'并更改配置:

'controllers' => array(
    'factories' => array(
         'Users\Controller\Users' => 'Users\Factory\UserControllerFactory'
     )
),

编写工厂类UserControllerFactory:

class UserControllerFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator){
        $realServiceLocator = $serviceLocator->getServiceLocator();
        $userService= $realServiceLocator->get('Users\Service\UserServiceInterface');

        return new UsersController($userService);
    }
}

UserService具有依赖关系:

'service_manager' => array(
    'factories' => array(
        'Users\Service\UserServiceInterface' => 'Users\Factory\UserServiceFactory',
        'Users\Mapper\PostMapperInterface'   => 'Users\Factory\ZendDbSqlMapperFactory',
     )
),

我也

Previous exceptions:
Zend\ServiceManager\Exception\ServiceNotCreatedException
File:
D:\wamp\www\AppZend2\vendor\zendframework\zend-servicemanager\src\ServiceManager.php:1101
Message:
While attempting to create usersserviceuserserviceinterface(alias: Users\Service\UserServiceInterface) an invalid factory was registered for this instance type.

这是我的UserServiceFactory:

use Users\Service\UserService;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class UserServiceFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        return new UserService(
            $serviceLocator->get('Users\Mapper\PostMapperInterface')
        );
    }
}

0 个答案:

没有答案