Zend Framework How can I print a logged in user name from a Zend_Session_Namespace
- by IrishStudent76
Hi all I have created the following login controller for my site and it works fine in relation to logging users in a logging them out. The thing I want to do is echo the logged in users name into the FlashMessenger for the success page how ever as my code stands I only get the following message when redirected to the success page, "you have been successfully logged in as Array".
Can I also ask the following does the line $session-user =$adaptergetResultArray('Password'); create an array of user information less the password value from the database.
Many Thanks in advance,
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('/');
}
}