So i’m using a simple Zend_Auth mechanism to ensure my users login before they
can access certain controllers. My AdminController contains this method
function preDispatch()
{
$auth = Zend_Auth::getInstance();
if (!$auth->hasIdentity()) {
$this->_redirect('auth/login');
}
}
The user gets redirected to the AuthController, but after a success login when get redirected to
default ‘index’ page for my site, and not the ‘admin’ page.
function loginAction()
{
if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
// collect the data from the user
Zend_Loader::loadClass('Zend_Filter_StripTags');
$filter = new Zend_Filter_StripTags();
$username = $filter->filter($this->_request->getPost('username'));
$password = $filter->filter($this->_request->getPost('password'));
if (empty($username)) {
$this->view->message = 'Please provide a username.';
} else {
// setup Zend_Auth adapter for a database table
$dbAdapter = Zend_Db_Table::getDefaultAdapter();
//Zend_Loader::loadClass('Zend_Auth_Adapter_DbTable');
$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
$authAdapter->setTableName('login');
$authAdapter->setIdentityColumn('email');
$authAdapter->setCredentialColumn('password');
// Set the input credential values to authenticate against
$authAdapter->setIdentity($username);
$authAdapter->setCredential($password);
// do the authentication
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($authAdapter);
if ($result->isValid()) {
// success : store database row to auth's storage system (not the password though!)
$data = $authAdapter->getResultRowObject(null, 'password');
$auth->getStorage()->write($data);
// I THINK I NEED TO CHANGE THIS LINE
$this->_redirect('/');
} else {
// failure: clear database row from session
$this->view->message = 'Login failed.';
}
}
}
$this->render();
}
What the correct way in Zend to configure the preDispatch() method so that i can tell the AuthController to redirect to page requested initially?
You can start from this one http://pastebin.com/f3472a877 and write your own. Usage: