I'm trying to adapt my ZF2 User
module to ZF3 MVC. It has an authentication service manager that is called in the onBootsrap
function inside the Module
class for every request (i.e. page loading) to check if the user is authenticated.
As serviceLocator
and ServiceAware
are not available anymore I'm trying to create an AuthenticationServiceFactory
but I do not succeed yet. Would you have any ideas on what I'm doing wrong and how I could do it with ZF3 ?
Here is a simplified version of my module/User/config.module.config.php
file
namespace User;
use ...
return [
'router' => [...],
'controllers' => [...],
'service_manager' => [
'factories' => [
Service\AuthenticationServiceFactory::class => InvokableFactory::class,
],
],
];
Here is my module/User/src/Service/AuthenticationServiceFactory.php
file
namespace User\Service;
use Interop\Container\ContainerInterface;
use Zend\Authentication\AuthenticationService;
use Zend\ServiceManager\Factory\FactoryInterface;
use Zend\Db\Adapter\Adapter as DbAdapter;
use Zend\Authentication\Adapter\DbTable\CredentialTreatmentAdapter as AuthAdapter;
use Zend\Authentication\Storage\Session as Storage;
class AuthenticationServiceFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$controllerPluginManager = $container;
$serviceManager = $controllerPluginManager->get('ServiceManager');
$config = $serviceManager->get('configuration');
$dbAdapter = new DbAdapter($config['db']); // Mysqli driver working in other modules
$authAdapter = new AuthAdapter($dbAdapter);
$authAdapter->setTableName('user')->setIdentityColumn('username')->setCredentialColumn('password');
$storage = new Storage();
return new AuthenticationService($storage, $authAdapter);
}
}
Here is my module/User/src/Module.php
file
namespace User\Service;
use Zend\Mvc\MvcEvent;
use Zend\Authentication\Adapter\DbTable\CredentialTreatmentAdapter;
class Module
{
public function getConfig()
{
return include __DIR__ . '/../config/module.config.php';
}
public function onBootstrap(MvcEvent $e)
{
$services = $e->getApplication()->getServiceManager();
$auth = $services->get(AuthenticationServiceFactory::class);
// Returns Fatal error: Call to undefined method Zend\Authentication\AuthenticationServiceFactory::setIdentity()
// $auth is an AuthenticationServiceFactory object and not the AuthenticationService returned by its __invoke() function
$this->authAdapter->setIdentity('dummy_user_name');
$this->authAdapter->setCredential('dummy_password');
print_r($this->authAdapter->authenticate());
}
}
Any ideas ?