2016-02-05 53 views
1

我試圖在ZF2應用程序中實現身份驗證模塊,我完全按照我在官方文檔中找到的方式執行,但是我得到這個錯誤:致命錯誤:調用未定義的方法DoctrineModule Authentication Adapter ObjectRepository :: setIdentityValue()

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

看看這個配置[Zend 2 + doctrine 2身份驗證適配器](http://stackoverflow.com/questions/12092091/zend-2-doctrine-2-auth-adapter) – ArrowHead

+0

我跟着但沒有幫幫我。我的配置看起來很不錯。看看它沒有找到這個函數: 'DoctrineModule \ Authentication \ Adapter \ ObjectRepository :: setIdentityValue()' –

回答

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 
) 
0

看看這個配置:

Zend2 + Doctrine2 Authentication

+0

我跟着但沒有幫助。我的配置看起來很不錯。看看它沒有找到這個函數:'DoctrineModule \ Authentication \ Adapter \ ObjectRepository :: setIdentityValue()' –

0

是的,我會確認點(和錯誤)亞瑟。 我剛更新到Zend 2.5,我的實際版本。 需要更改方法的setIdentityValue和setCredentialValue: - setIdentity() - setCredential()。 有了這個改變我的代碼工作正常。

相關問題