I try to create a module Users I am inspire from the Blog Exemple of zend 2.4, I build a factory to instantiate my controller UsersController, but i get this error:
An exception was raised while creating "Users\Controller\Users"; no instance returned
Actually the problem arise when I bring the service into the controler
class UsersController extends AbstractActionController {
protected $userService;
public function __construct(UserServiceInterface $userService){
$this->userService = $userService ;
}
}
Then I defined UserServiceInterface and create an instance of the 'Users\Controller\UserController' and change the configuration:
'controllers' => array(
'factories' => array(
'Users\Controller\Users' => 'Users\Factory\UserControllerFactory'
)
),
And Writing a Factory Class UserControllerFactory :
class UserControllerFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator){
$realServiceLocator = $serviceLocator->getServiceLocator();
$userService= $realServiceLocator->get('Users\Service\UserServiceInterface');
return new UsersController($userService);
}
}
The UserService has a dependency :
'service_manager' => array(
'factories' => array(
'Users\Service\UserServiceInterface' => 'Users\Factory\UserServiceFactory',
'Users\Mapper\PostMapperInterface' => 'Users\Factory\ZendDbSqlMapperFactory',
)
),
I get also
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.
this is my 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')
);
}
}