2011-05-28 43 views
0

我是this教程,在我的應用程序中實現登錄系統。這是給我下面的過程動作錯誤:如何在zend中獲取認證適配器?

Message: Method "getAuthAdapter" does not exist and was not trapped in __call() 

上以下行:

$adapter = $this->getAuthAdapter($form->getValues()); 

所以現在我要實現getAuthAdapter()函數,但如何在這個功能代碼。

感謝

+0

馬修說,在教程的頂部:「對於這一切工作,你需要一個認證適配器。我不打算詳細說明這一點,因爲文檔涵蓋了它們,而且您的需求會根據您的網站而有所不同。但是,我將假設您的身份驗證適配器需要用戶名和密碼來提供身份驗證憑據。 「http://framework.zend.com/manual/en/zend.auth.html#zend.auth.introduction.adapters – Rufinus 2011-05-28 16:18:40

回答

2

創建您自己的身份驗證功能:

protected function _getAuthAdapter($userLogin, $userPass){ 
    $authAdapter = new Zend_Auth_Adapter_DbTable(
     Zend_Db_Table::getDefaultAdapter(), 
     'user', //db table name 
     'login', // identity column 
     'password' // credential column 
    ); 
    $authAdapter->setIdentity($userLogin)->setCredential($userPass); 
    return $authAdapter; 
} 

數據庫驗證文檔,你可以找到here

對於其他適配器看看here

1

你可以,如果你想通過數據庫來驗證您可以使用此代碼使用此example

0

所以這是我完整的解決方案:

<?php 

class AuthenticationController extends Zend_Controller_Action { 

    public function init() { 
     /* Initialize action controller here */ 
    } 

    public function indexAction() { 
     // action body 
    } 

    /** 
    * Show login form 
    */ 
    public function loginAction() { 

     $this->view->heading = 'Login'; 
     $this->view->form = new Form_Login(); 
     $this->view->form->setAction('process'); 
    } 

    /** 
    * preDispatch: If user is already logged in then 
    * redirect to index, if not then redirect to login 
    */ 
    public function preDispatch() { 

     if (Zend_Auth::getInstance()->hasIdentity()) { 
      // If the user is logged in, we don't want to show the login form; 
      // however, the logout action should still be available 
      if ('logout' != $this->getRequest()->getActionName()) { 
       $this->_helper->redirector('index', 'index'); 
      } 
     } else { 
      // If they aren't, they can't logout, so that action should 
      // redirect to the login form 
      if ('logout' == $this->getRequest()->getActionName()) { 
       $this->_helper->redirector('login'); 
      } 
     } 
    } 

    /** 
    * Provide authentication adapter 
    * 
    * @param unknown_type $values 
    */ 
    public function getAuthAdapter($values) { 

     $authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter(), 
                 'Authentication', 
                 'username', 
                 'password', 
                 'MD5(CONCAT(salt,?))' 
                ); 

     $authAdapter->setIdentity($values['username']); 
     $authAdapter->setCredential($values['password']);           

     return $authAdapter; 
    } 

    /** 
    * Process login request. If user is authenticated 
    * then redirect to index otherwise show login form again 
    */ 
    public function processAction() { 

     $request = $this->getRequest(); 

     // Check if we have a POST request 
     if (!$request->isPost()) { 
      return $this->_helper->redirector('login'); 
     } 

     // Get our form and validate it 
     $form = new Form_Login(); 
     if (!$form->isValid($request->getPost())) { 
      // Invalid entries 
      $this->view->form = $form; 
      return $this->render('login'); // re-render the login form 
     } 

     // Get login form values 
     $values = $form->getValues(); 

     // Get our authentication adapter and check credentials 
     $adapter = $this->getAuthAdapter($values); 
     $auth = Zend_Auth::getInstance(); 
     $result = $auth->authenticate($adapter); 

     if (!$result->isValid()) { 
      // Invalid credentials 
      $form->setDescription('Invalid credentials provided'); 
      $this->view->form = $form; 
      $this->_helper->redirector('login'); // re-render the login form 
     } 

     // Create session 
     $session = new Zend_Session_Namespace('user'); 
     $session->userEmail = $values['username']; 
     $session->userId = '1'; 

     // We're authenticated! Redirect to the home page 
     $this->_helper->redirector('index', 'index'); 
    } 

    /** 
    * logout request 
    */ 
    public function logoutAction() { 

     // Destroy session 
     $session = new Zend_Session_Namespace('user'); 
     $session->userEmail = null; 
     $session->userId = null; 

     Zend_Auth::getInstance()->clearIdentity(); 
     $this->_helper->redirector('login'); // back to login page 
    } 


} // end class 


?> 
相關問題