我正在尝试在ZF2应用程序中实现身份验证模块,我完全按照我在官方文档中找到的那样,但是我收到了这个错误:
Fatal error: Call to undefined method DoctrineModule\Authentication\Adapter\ObjectRepository::setIdentityValue() in DOCROOT/module/Login/src/Login/Controller/IndexController.php on line 33
我把它放在我的module.config.php中:
'doctrine' => array(
'driver' => array(
__NAMESPACE__ . '_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
)
)
),
'authentication' => array(
'orm_default' => array(
'object_manager' => 'Doctrine\ORM\EntityManager',
'identity_class' => 'Login\Entity\User',
'identity_property' => 'email',
'credential_property' => 'password',
),
),
)
在我的module.php中:
public function getServiceConfig()
{
return array(
'factories' => array(
'Zend\Authentication\AuthenticationService' => function($serviceManager) {
return $serviceManager->get('doctrine.authenticationservice.orm_default');
}
)
);
}
这是我的控制者:
namespace Login\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController{
public function indexAction(){
if ($this->getRequest()->isPost()) {
if($this->authenticate()){
return new ViewModel(array(
'error' => 'Your authentication credentials is valid!',
));
}else{
return new ViewModel(array(
'error' => 'Your authentication credentials are not valid',
));
}
}else{
return new ViewModel(array('error' => ''));
}
}
public function authenticate(){
$data = $this->getRequest()->getPost();
$authService = $this->getServiceLocator()->get('Zend\Authentication\AuthenticationService');
$adapter = $authService->getAdapter();
$adapter->setIdentityValue($data['email']);
$adapter->setCredentialValue($data['password']);
$authResult = $authService->authenticate();
if ($authResult->isValid()) {
return true;
}else{
return false;
}
}
}
任何胶水?
答案 0 :(得分:8)
好的,我找到了答案。似乎方法setIdentityValue()和setCredentialValue()(getters也)不再存在。而不是设置这些值,您必须调用setIdentity()和setCredential()。 似乎这最近发生了变化,因为我一无所知,而我的应用程序曾经工作过,突然间,停止工作导致这些变化。 这就是我意识到方法改变了他们的标志:
$authService = $this->getServiceLocator() >get('Zend\Authentication\AuthenticationService');`
$adapter = $authService->getAdapter();
$class_methods = get_class_methods($adapter);
echo "<pre>";print_r($class_methods);exit;
输出:
Array
(
[0] => __construct
[1] => setOptions
[2] => getOptions
[3] => authenticate
[4] => getCredential
[5] => setCredential
[6] => getIdentity
[7] => setIdentity
)
答案 1 :(得分:0)
看看这个配置:
答案 2 :(得分:0)
是的,我确认你的观点(和错误)亚瑟。 我刚刚更新到Zend 2.5,我的实际版本。 方法&#39; setIdentityValue&#39;和&#39; setCredentialValue&#39;需要改变: - setIdentity() - setCredential()。 通过此更改,我的代码工作正常。