Jump to content

zf login/logout with modules help


sasori

Recommended Posts

I have a problem , the question is a the bottom, but first let me show you the directory, routing, code structure of my practice app

 

xndk5f.jpg

 

This is the Member_LoginController Code

 

class Member_LoginController extends Zend_Controller_Action
{

public function init(){}
/*
 * index actin where the main login
 * form resides
 */
public function indexAction()
{
	$form = $this->getLoginForm();

	if($this->getRequest()->isPost())
	{
		if($form->isValid($this->getRequest()->getPost()))
		{
			if($this->_process($form->getValues()))
			{
				$this->_helper->redirector('index','index');
			}				
		}
	}
	$this->view->form = $form;		
}




public function getLoginForm()
{

	$form = new Zend_Form();
	$form->setAttrib('sitename', 'Noob');
	//login form;
	$myElements = new Noob_Form_Register();
	$form->addElement($myElements->getLoginField());

	//password field
	$form->addElement($myElements->getPasswordField());

	$form->addElement('submit','submit');
	$submitButton = $form->getElement('submit');
	$submitButton->setLabel('Login');

	$form->addDisplayGroup(array('loginName','password','submit'),'login');
	$form->getDisplayGroup('login')
		 ->setOrder(10)
		 ->setLegend('Member Login');

	return $form;

}


/*
 * main process for the main login
 */
protected function _process($values)
{
	$adapter = $this->_getAuthdapter();
	$adapter->setIdentity($values['loginName']);
	$adapter->setCredential($values['password']);

	$auth = Zend_Auth::getInstance();
	$result = $auth->authenticate($adapter);
	if($result->isValid())
	{
		$user = $adapter->getResultRowObject();
		$auth->getStorage()->write($user);
		return true;
	}
	return false;
}



/*
 * get auth adapter for the main login
 */
protected function _getAuthdapter()
{
	$dbAdapter = Zend_Db_Table::getDefaultAdapter();
	$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);

	$authAdapter->setTableName('members')
				->setIdentityColumn('LoginName')
				->setCredentialColumn('LoginPassword')
				->setCredentialTreatment('SHA1(CONCAT(?,salt))');

	return $authAdapter;
}


/*
 * main logout
 */
public function logoutAction()
{
	Zend_Auth::getInstance()->clearIdentity();
	Zend_Session::destroy();
	$this->_redirect('member/login');
}
}

 

 

here's the Member_IndexController Code

 

class Member_IndexController extends Zend_Controller_Action
{
public function preDispatch()
{
	//set member gateway layout
	$url = $this->getRequest()->getRequestUri();
	$this->_helper->layout->setLayout('member');
}

public function init(){}

public function indexAction()
{}

 

and here's a helper  /* credit goes to rob allen */

 

<?php 
class Zend_View_Helper_LoggedInAs extends Zend_View_Helper_Abstract
{
public function loggedInAs()
{
	$auth = Zend_Auth::getInstance();
	if ($auth->hasIdentity()) {
		$username = $auth->getIdentity()->WSLoginName;
		$logoutUrl = $this->view->url(array('controller' => 'login',
		'action' => 'logout', 'module' => 'member'), null, true);
		return 'Welcome '. $username . '. <a href="'. $logoutUrl . '">Logout</a>';
	}

	$request = Zend_Controller_Front::getInstance()->getRequest();
	$controller = $request->getControllerName();
	$module = $request->getModuleName();
	$action = $request->getActionName();
	if($controller == 'login' && $action == 'index'){
		return '';
	}

	$loginUrl = $this->view->url(array('controller' => 'login', 'action' => 'index'));
	return '<a href="'. $loginUrl . '">Login</a>';
}
}
?>

 

 

now here's the layout

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>Member Gateway</title>
<link rel="stylesheet" type="text/css" href="<?php echo $this->baseUrl('/css/member.css'); ?>" />
</head>
<body>
<div id="container">
<div id="header">
	<div id="menu">
		<ul>
			<li><a href="<?php echo $this->url(array(),'member-account'); ?>">My Account</a></li>
			<li><a href="<?php echo $this->url(array(),'member-details');?>">Membership Details</a></li>
			<li><a href="<?php echo $this->url(array(),'member-staff'); ?>">Members Staff</a></li>
			<li><a href="#">Members Account</a></li>
			<li><a href="#">Members Products</a></li>
			<li><a href="#">WSM</a></li>
			<li><a href="#">Web Products</a></li>
		</ul>
</div><!-- end menu -->
	<div align="right" id="login"><?php echo $this->loggedInAs(); ?></div>
</div><!-- end header -->
<div id="content">
	<?php echo $this->layout()->content; ?>
</div><!-- end content -->
<hr />
<div align="center" id="footer">
	<img src="<?php echo $this->baseUrl('/images/zendframeworklogo1.png'); ?>" />
</div><!-- end footer -->
</div><!-- end container -->
</body>
</html>

 

and here's the new Member_DetailsController Account code

 

class Member_DetailsController extends Zend_Controller_Action
{
public function preDispatch()
{
	//set member gateway layout
	$url = $this->getRequest()->getRequestUri();
	$this->_helper->layout->setLayout('member');
}

public function indexAction()
{

}

 

So here's what my problem is,

1 ) when  I log in, i get redirected to the IndexController of the member module, so, the "logout" link works there

2) when I click a link within that index view that points to another module controller (Details controller) , the logout doesn't work .

- I am using firebug to see the session/cookies. I don't know why it disappears once I go to another controller,

 

1) what should I do in order to make this logout helper work in all the additional controllers within the same modules that I am about to create ?

- Is there a problem with my routing ?

 

:please answer my question or post some useful code snippets that can help me accomplish this objective .

thanks in advance  :confused:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.