【发布时间】:2013-09-26 09:58:35
【问题描述】:
我通过在Module.php 中创建getServiceConfig() 类似AuthenticationService 的对象来实现Zend Auth:
'AuthService' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter, 'tblUSRUsers', 'Email', 'Password', "MD5(?)");
$authService = new AuthenticationService();
$authService->setAdapter($dbTableAuthAdapter);
$authService->setStorage($sm->get('Application\Model\MyAuthStorage'));
return $authService;
},
在控制器动作中,我通过
获取这个对象$this->getServiceLocator()
->get('AuthService');
对于身份验证,我使用
获取值$authservice = $this->getServiceLocator()->get('AuthService');
$arrUserDetails = $authservice->getIdentity();
它工作正常,这些值可用。
但问题是 ServiceLocator 在 Controller 构造函数中不可用,因此无法在那里编写上述代码。在每个动作中编写此代码似乎不是一个好习惯。
有人可以帮忙吗?
【问题讨论】:
标签: php zend-framework2 zend-auth service-locator