I am struggling from last 2 days with the following issue but still not able to resolve it.
With the help of http://akrabat.com/zend-auth-tutorial/ I am doing an authentication module using Zend_Auth in Zend Framework. After successful authentication I am storing users data using getStorage();
Example :
AuthController.php
In method _process I have written like this :
protected function _process($values) {
// Get our authentication adapter and check credentials
$adapter = $this->_getAuthAdapter();
$adapter->setIdentity($values['username']);
$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;
}
print_r($user) showing the result :
stdClass Object ( [id] => 1 [username] => admin [password] => cb3aefbdffbc81588f3d43c394428b16d4346b44 [salt] => ce8d96d579d389e783f95b3772785783ea1a9854 [role] => administrator [date_created] => 2012-12-29 11:04:40)
Also,
print_r($auth->getStorage()->read());
This also displaying me the result.
Now, I want to display Logout link if user successfully logged in.
view\helpers\LoggedInAs.php
class Zend_View_Helper_LoggedInAs extends Zend_View_Helper_Abstract {
public function loggedInAs() {
$auth = Zend_Auth::getInstance();
if ($auth->hasIdentity()) {
$username = $auth->getIdentity()->username;
$logoutUrl = $this->view->url(array('controller' => 'auth',
'action' => 'logout'), null, true);
return 'Welcome ' . $username . '. <a href="' . $logoutUrl . '">Logout</a>';
}
$request = Zend_Controller_Front::getInstance()->getRequest();
$controller = $request->getControllerName();
$action = $request->getActionName();
if ($controller == 'auth' && $action == 'index') {
return '';
}
$loginUrl = $this->view->url(array('controller' => 'login', 'action' => 'index'));
return '<a href="' . $loginUrl . '">Login</a>';
}
}
But its not calling the method hasIdentity thats why its not going to the if block and
print_r($auth) showing output like this:
Zend_Auth Object ( [_storage:protected] => )
If I am doing print_r($_SESSION); then also it showing blank array. The issue is $_SESSION is working in controllers and modules but in view/helpers it displaying blank array.
Bootstrap.php
protected function _initSession() {
try {
Zend_Session::start();
} catch (Zend_Session_Exception $e) {
session_start();
}
}
I am using Zend Framework version 1.11.
I am not sure this is the issue of Session or Zend_Auth::getInstance() or view helpers.
Because of this issue I am not able go forward for study Zend_ACL module. How can I fix this.
Any help would be appreciated.
Thanks in advance