大家好我已經爲我的網站創建了以下登錄控制器,並且它在記錄用戶登錄時可以正常工作。我想要做的事情是將登錄的用戶名稱回傳到成功頁面的FlashMessenger中,就像我的代碼所呈現的一樣,當重定向到成功頁面時,我只收到以下消息,「您已成功以Array的身份登錄」。 我也可以問下面的行$ session-> user = $ adapter> getResultArray('Password');創建一個用戶信息數組,而不是數據庫中的密碼值。Zend Framework如何從Zend_Session_Namespace打印已登錄的用戶名
許多在此先感謝,
IrishStudent76
<?php
class LoginController extends Zend_Controller_Action
{
public function init(){
$this->view->doctype('XHTML1_STRICT');
}
// login action
public function loginAction()
{
$form = new PetManager_Form_Login;
$this->view->form = $form;
/*
check for valid input from the form and authenticate using adapter
Add user record to session and redirect to the original request URL if present
*/
if ($this->getRequest()->isPost()) {
if ($form->isValid($this->getRequest()->getPost())) {
$values = $form->getValues();
$adapter = new PetManager_Auth_Adapter_Doctrine(
$values['username'], $values['password']
);
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($adapter);
if ($result->isValid()) {
$session = new Zend_Session_Namespace('petmanager.auth');
$session->user = $adapter->getResultArray('Password');
if (isset($session->requestURL)) {
$url = $session->requestURL;
unset($session->requestURL);
$this->_redirect($url);
} else {
$this->_helper->getHelper('FlashMessenger')
->addMessage('You have been successfully logged in as '.$session- >user);
$this->_redirect('/login/success');
}
} else {
$this->view->message = 'You could not be logged in. Please try again.';
}
}
}
}
public function successAction()
{
if ($this->_helper->getHelper('FlashMessenger')->getMessages()) {
$this->view->messages = $this->_helper
->getHelper('FlashMessenger')
->getMessages();
} else {
$this->_redirect('/login');
}
}
public function logoutAction()
{
Zend_Auth::getInstance()->clearIdentity();
Zend_Session::destroy();
$this->_redirect('/');
}
}
爲什麼你需要一個單獨的會話來存儲有關用戶的登錄信息?你不能將它存儲在auth會話中,例如。 '$ auth-> getStorage() - > write($ whatEverYouWantToStore);'? – Marcin 2011-03-19 00:02:35