您应该使用 Zend_Auth 进行身份验证。
有关它的更多信息,您可以在此处阅读:manual.zfdes.com/de/zend.auth.html
这就是我使用 Zend_Auth 的方式:
$auth = Zend_Auth::getInstance();
$authAdapter = new Altergear_Seolista_UserLoginAuthAdapter(
$loginForm->getLogin(),
$loginForm->getPassword()
);
$result = $auth->authenticate($authAdapter);
认证适配器返回一个带有新身份的新 Zend_Auth_Result。对于那些我使用 stdClass 对象。它将取代旧的身份。
这里以我的适配器为例
<?php
使用 Doctrine\ORM\EntityManager;
类 Altergear_MyApp_UserLoginAuthAdapter 实现 Zend_Auth_Adapter_Interface
{
/**
* 登录
* @var 字符串 $_login
*/
私人 $_login;
/**
* The user password
* @var string $_password
*/
private $_password;
/**
* The Doctrine EntityManager
* @var EntityManager $_em
*/
private $_em;
public function __construct( $email, $password )
{
$registry = Zend_Registry::getInstance();
$this->_em = $registry->entitymanager;
$this->_login = $email;
$this->_password = $password;
}
/**
* Executes an authentication try
*
* @throws Zend_Auth_Adapter_Exception if the authentication failed
* @return Zend_Auth_Result
*/
public function authenticate()
{
//check if login is correct
try{
$user = $this->_em->getRepository('App_Model_User')
->findOneBy(
array(
'_email' => $this->_login,
'_password' => App_Model_User::encodePassword( $this->_password )
)
);
}catch( Exception $e ){
throw new Zend_Auth_Adapter_Exception( 'authentication failed', 0, $e );
}
if( $user!==null && $user instanceof App_Model_User && $user->getId() > 0 )
{
//login successful
$identity = new stdClass();
$identity->user = $user;
return new Zend_Auth_Result( Zend_Auth_Result::SUCCESS , $identity );
}else{
//login failed
return new Zend_Auth_Result( Zend_Auth_Result::FAILURE, NULL, array('Bitte geben Sie gültige Logindaten an.') );
}
最重要的是,您可以使用 Zend_Acl 我推荐您使用 Zend_Controller 插件来管理对您的控制器和操作的访问。