2013-11-26 62 views
1

我想做一個Zend 2經典身份驗證,而不使用DbTable檢查。沒有DbTable檢查Zend Framework 2身份驗證

我會更好地解釋

  • 我有一個自定義的方法進行身份驗證(),它能夠對用戶進行驗證(獨立於Zend的驗證)。
  • 我做了擴展的Zend \認證\存儲\會話的自定義類
  • 現在我想將數據保存session像這樣的東西:

    $authService->getStorage()->write($this); 
    

的問題是使用該代碼在下一頁呼叫時執行:

if($authService->hasIdentity()) 

它的響應爲false。

那麼,我該如何使用自定義身份驗證來保存身份?我想我可以實現的Zend \認證\適配器\ AdapterInterface但我不知道exacly

任何幫助是怎麼...讚賞, 非常感謝:)

回答

3

您可以創建像這樣的適配器:

use Zend\Authentication\Adapter\AdapterInterface; 
use Zend\Authentication\Result as AuthResult; 

class MyAdapter implements AdapterInterface 
{ 

    /** 
    * @return \Zend\Authentication\Result 
    */ 
    public function authenticate() 
    { 
     /* Return if can't find user */ 
     return new AuthResult(AuthResult::FAILURE_IDENTITY_NOT_FOUND, null); 
     /* Return if success, second parameter is the identity, e.g user. */ 
     return new AuthResult(AuthResult::SUCCESS, $identity); 
     /* Return if user found, but credentials were invalid */ 
     return new AuthResult(AuthResult::FAILURE_CREDENTIAL_INVALID, null); 
    } 
} 

您可以使用該適配器與ZF2的AuthenticationService像這樣:

$auth_service = new \Zend\Authentication\AuthenticationService(); 
/* Where $myAdapter is a instance of the MyAdapter class above */ 
$auth_service->setAdapter($myAdapter); 
+0

感謝很多:) – user2540463