antoniofh (1) [Avatar] Offline
#1
Hello,

I am trying to create a login system for my application using the Zend_Auth examples in ZFIA and ZendFramework v1.10.1. I have created this application using the book as a guide, however customized extensively for my needs. I believe I have everything setup properly, however when I attempt to hit my homepage after adding the following code to my index controllers preDispatch I get an inifinite loop error:

$auth = Zend_Auth::getInstance();

if (!$auth->hasIdentity()) {
$this->_redirect('auth/form');
}

If I move the above code to the index action of my index controller I do not have any issues being redirected to /auth/form, however that's obviously not a usable solution.

After a week or so of troubleshooting I have finally narrowed the issue down to my ActionSetup.php file. As soon as I comment out the line: $front->registerPlugin($actionStack,97); the redirect loop goes away... however I then no longer have my navigation menu smilie

Here is my full indexController:

<?php <br />
class IndexController extends Zend_Controller_Action
{
function preDispatch()
{
$auth = Zend_Auth::getInstance();

if (!$auth->hasIdentity()) {
$this->_redirect('auth/form');
}
}

public function init()
{
$this->view->user = Zend_Auth::getInstance()->getIdentity();
}

public function indexAction()
{
$this->view->title = 'Reporting Database';
$this->view->headTitle($this->view->title, 'PREPEND');
}

public function menuAction() {
$mainMenu = array(
array('title'=>'Home', 'url'=>$this->view->url(array(), null, true)),
array('title'=>'Administration', 'url'=>$this->view->url(array('controller'=>'administration'), null, true)),
array('title'=>'Finance', 'url'=>$this->view->url(array('controller'=>'finance'), null, true)),
array('title'=>'Purchasing', 'url'=>$this->view->url(array('controller'=>'purchasing'), null, true)),
array('title'=>'Sales', 'url'=>$this->view->url(array('controller'=>'sales'), null, true)),
array('title'=>'Warehouse', 'url'=>$this->view->url(array('controller'=>'warehouse'), null, true)),
);

$this->view->menu = $mainMenu;
$this->_helper->viewRenderer->setResponseSegment('menu');
}

public function serviceAction() {
$this->_helper->viewRenderer->setResponseSegment('service');
}

}


Here is my AuthController:

<?php <br />
class AuthController extends Zend_Controller_Action
{

protected $_redirectUrl = '/';

public function init()
{
$this->_helper->layout->setLayout('login');
}

public function indexAction()
{
$this->_redirect('/');
}

public function formAction()
{
$form = new Application_Form_LoginForm();
$form->setAction($this->view->baseUrl() .'/auth/form/');
$this->view->formResponse = '';
if ($this->getRequest()->isPost()) {
if ($form->isValid($_POST)) {
// Retrieve the auth adapter from Reports_Validate_Authorise
// via the username form element
$authAdapter = $form->username->getValidator('Authorise')->getAuthAdapter();

// success: store database row to auth's storage
// (Not the password though!)
$data = $authAdapter->getResultRowObject(null,
'password');
$auth = Zend_Auth::getInstance();
$auth->getStorage()->write($data);
$this->_redirect($this->_redirectUrl);
} else {
// ensure that the Auth adapter is cleared
$auth = Zend_Auth::getInstance();
$auth->clearIdentity();

$this->view->formResponse = 'Sorry, there was a problem with your submission. Please check the following:';
$form->populate($_POST);
}
}

$this->view->form = $form;
}

public function logoutAction()
{
$auth = Zend_Auth::getInstance();
$auth->clearIdentity();
$this->_redirect('/');
}
}

And here is my full ActionSetup.php:

<?php <br />
class Reports_Controller_Plugin_ActionSetup extends Zend_Controller_Plugin_Abstract {

public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {

if(!$request->isXmlHttpRequest()) {
$front = Zend_Controller_Front::getInstance();
if (!$front->hasPlugin('Zend_Controller_Plugin_ActionStack')) {
$actionStack = new Zend_Controller_Plugin_ActionStack();
$front->registerPlugin($actionStack,97);
} else {
$actionStack = $front->getPlugin('Zend_Controller_Plugin_ActionStack');
}

$menuAction = clone($request);
$menuAction->setActionName('menu')->setControllerName('index');
$actionStack->pushStack($menuAction);

$serviceAction = clone($request);
$serviceAction->setActionName('service')->setControllerName('index');
$actionStack->pushStack($serviceAction);
}

}
}

And finally here are the applicable lines in my application.ini

phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
phpSettings.date.timezone = "America/Edmonton"
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
autoloaderNamespaces[] = "Reports_"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.frontController.plugins.ActionSetup = Reports_Controller_Plugin_ActionSetup
resources.frontController.plugins.ViewSetup = Reports_Controller_Plugin_ViewSetup
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"

My Bootstrap.php is the default as setup by the zf tool. (Pretty much blank).

Any help/advice would be appreciated on this one. Like I said I've been working on it for at least a week now smilie