我正在尝试将我的ZF2 User
模块改编为ZF3 MVC . 它有一个身份验证服务管理器,在 Module
类中的 onBootsrap
函数中为每个请求(即页面加载)调用,以检查用户是否经过身份验证 .
由于 serviceLocator
和 ServiceAware
不再可用,我正在尝试创建 AuthenticationServiceFactory
但我还没有成功 . 您对我的错误以及如何使用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());
}
}
有任何想法吗 ?
2 回答
像往常一样,当我自己找到答案时,我在这里发布它可以帮助某人 .
module/User/config.module.php
module/User/src/Service/AuthenticationServiceFactory.php
module/User/src/Module.php
我的问题是设置用户的身份和凭证不应该在这里完成,而是在其他地方设置
login()
功能 . 在Module Class
中我们只需要检查身份 .我赞同你自己的答案,因为我认为这是一个很好的答案,但也许这会改善数据库连接 .
您写道,您已经有一个Mysqli驱动程序与另一个模块一起工作 . 如果在用户表的另一个模块中有一个存储库,则可以使用它并使用自定义适配器简化代码 . 假设您的用户存储库实现
User\Model\UserRepositoryInterface
:Module\src\Factory\CustomAdapterFactory.php
你的 AuthenticationServiceFactory 成了:
注册您的工厂: module/User/config.module.php