Php 如何在zend中获取身份验证适配器?

Php 如何在zend中获取身份验证适配器?,php,zend-framework,authentication,Php,Zend Framework,Authentication,我正在学习如何在我的应用程序中实现登录系统。在流程操作中,它给了我以下错误: Message: Method "getAuthAdapter" does not exist and was not trapped in __call() 在下面一行: $adapter = $this->getAuthAdapter($form->getValues()); 所以现在我必须实现getAuthAdapter()函数,但是如何在这个函数中编码 谢谢您可以使用此创建您自己的身份验证功能

我正在学习如何在我的应用程序中实现登录系统。在流程操作中,它给了我以下错误:

Message: Method "getAuthAdapter" does not exist and was not trapped in __call() 
在下面一行:

$adapter = $this->getAuthAdapter($form->getValues());
所以现在我必须实现getAuthAdapter()函数,但是如何在这个函数中编码


谢谢

您可以使用此创建您自己的身份验证功能

您可以使用此创建您自己的身份验证功能

如果您想通过数据库进行身份验证,您可以使用以下代码:

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;
}
您可以找到的数据库身份验证文档


对于其他适配器,请查看

如果要通过数据库进行身份验证,可以使用以下代码:

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;
}
您可以找到的数据库身份验证文档


对于其他适配器,请查看,这是我的完整解决方案:

<?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


?>

所以这是我的完整解决方案:

<?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


?>


正如matthew在教程顶部所说:“要使所有这些都起作用,您需要一个身份验证适配器。我不打算详细讨论这一点,因为文档涵盖了它们,您的需求将根据您的站点而有所不同。但是,我将假设您的身份验证适配器需要用户名和密码作为身份验证凭据。正如matthew在教程顶部所说:“要使所有这些都起作用,您需要一个身份验证适配器。我不打算详细讨论这一点,因为文档涵盖了它们,您的需求将根据您的站点而有所不同。但是,我将假设您的身份验证适配器需要用户名和密码作为身份验证凭据。"