我是this教程,用于在我的应用程序中实现登录系统。它在流程操作中给出了以下错误:
Message: Method "getAuthAdapter" does not exist and was not trapped in __call()
在以下行:
$adapter = $this->getAuthAdapter($form->getValues());
所以现在我必须实现getAuthAdapter()函数,但是如何在这个函数中编码。
由于
答案 0 :(得分: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 :(得分:1)
您可以使用此example
创建自己的身份验证功能答案 2 :(得分: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
?>