2010-07-25 34 views
1

目前,登陸在Zend框架用戶,我這樣做在Zend_Validate而不是控制器操作中執行認證認證?

public function loginAction() 
{ 
    if ($this->getRequest()->isPost()) { 
     $adapter = new Application_Auth_Adapter(
         $this->getRequest()->getParam('username'), 
         $this->getRequest()->getParam('password') 
        ); 
     $auth = Zend_Auth::getInstance(); 
     $auth->authenticate($adapter); 
     if ($auth->hasIdentity()) { 
      echo $auth->getIdentity()->name; 
     } else { 
      echo "failed login"; 
     } 
    } else { 
     echo "not posted"; 
    } 
} 

,但我想知道如果我SHLD都驗證邏輯把Zend_Validate不是那麼所有內部

$auth = Zend_Auth::getInstance(); 
$auth->authenticate($adapter); 
if ($auth->hasIdentity()) { ... 

我控制器做的是檢查表格是否爲isValid()?大多數教程做控制器的驗證,但我想知道,因爲驗證用戶登錄聽起來像是驗證我...

回答

2

我認爲行爲驗證用戶是分開的驗證用戶。

您可以擁有一個驗證程序,可用於測試某組證書是否有效。但是,這通常會有點多餘,因爲您可以簡單地將憑證傳遞給Zend_Auth以確定有效性如果是這樣,則對用戶進行身份驗證。

如果您希望從您的控制器邏輯中分離您的登錄過程(可能比調用Zend_Auth更復雜,或者您需要在多個位置執行此操作),則可以創建登錄服務類它抽象出必要的步驟。