2011-03-18 54 views
0

大家好我已經爲我的網站創建了以下登錄控制器,並且它在記錄用戶登錄時可以正常工作。我想要做的事情是將登錄的用戶名稱回傳到成功頁面的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('/'); 
} 

} 
+1

爲什麼你需要一個單獨的會話來存儲有關用戶的登錄信息?你不能將它存儲在auth會話中,例如。 '$ auth-> getStorage() - > write($ whatEverYouWantToStore);'? – Marcin 2011-03-19 00:02:35

回答

1

這是你設置$會話級>的用戶,您稍後嘗試融入消息:

$session->user = $adapter->getResultArray('Password'); 

由於你可以看到,你正在得到一個結果數組,所以難怪$ session-> user存儲一個數組。你爲什麼不使用$auth->getIdentity()來獲取用戶名?

而且我會考慮閱讀the manual,它以一種非常清晰的方式顯示了一切!

+0

如果它是一個數組,那麼' - > user'不會起作用。 – Phil 2011-03-19 00:06:48

+0

取決於,也許它只是被稱爲數組,但實際上是一個流體接口。 – markus 2011-03-19 00:11:05

+0

這顯然是一個數組,否則OP將不會「以數組形式登錄」 – Phil 2011-03-19 00:13:31

0

你不應該需要推出自己的會話auth存儲,這是全部內置到Zend_Auth

嘗試這樣的事情...

if ($result->isValid()) { 
    $identity = $adapter->getResultArray('Password'); 
    $auth->getStorage()->write($identity); 
    $session = new Zend_Session_Namespace('petmanager.auth'); 
    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 '.$identity['NameColumn']); 
    $this->_redirect('/login/success'); 
    } 
} else { 
    $this->view->message = 'You could not be logged in. Please try again.';   
}