I supported your own answer because I think it is good, but maybe this will improve it for connecting to DB.
You wrote that you already have a Mysqli driver that works with another module. If you have a repository in this other module for a user table, you can use it and simplify the code using a custom adapter. Suppose your user repository implements User\Model\UserRepositoryInterface :
namespace User\Model; interface UserRepositoryInterface { public function getUser($id); public function updateUser(User $user);
Module\src\Factory\CustomAdapterFactory.php
namespace User\Factory; use Interop\Container\ContainerInterface; use User\Adapter\CustomAdapter; use User\Model\UserRepositoryInterface; use Zend\ServiceManager\Factory\FactoryInterface; class CustomAdapterFactory implements FactoryInterface { public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { return new CustomAdapter($container->get(UserRepositoryInterface::class)); } }
Your AuthenticationServiceFactory will become:
namespace User\Factory; use Interop\Container\ContainerInterface; use User\Adapter\CustomAdapter; use Zend\Authentication\AuthenticationService; use Zend\ServiceManager\Factory\FactoryInterface; use Zend\Authentication\Storage\Session as SessionStorage; class AuthenticationServiceFactory implements FactoryInterface { public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { $authService = new AuthenticationService(); $storage = new SessionStorage(); $authService->setStorage($storage); $authService->setAdapter($container->get(CustomAdapter::class)); return $authService; } }
Register your factories: module/User/config.module.php
namespace User; use User\Factory\AuthenticationServiceFactory; use User\Factory\CustomAdapterFactory; use User\Factory\UserRepositoryFactory; use Zend\Authentication\AuthenticationService; return [ 'service_manager' => [ 'aliases' => [ Model\UserRepositoryInterface::class => Model\UserRepository::class ], 'factories' => [ Model\UserRepository::class => UserRepositoryFactory::class, Adapter\CustomAdapter::class => CustomAdapterFactory::class, MailService::class => MailServiceFactory::class, AuthenticationService::class => AuthenticationServiceFactory::class, ] ],
source share