所以我使用简单的Zend_Auth机制来确保我的用户在访问某些控制器之前登录.我的AdminController包含此方法
function preDispatch() { $auth = Zend_Auth::getInstance(); if (!$auth->hasIdentity()) { $this->_redirect('auth/login'); } }
用户被重定向到AuthController,但在成功登录后被重定向到我的网站的默认"索引"页面,而不是"管理员"页面.
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(); }
Zend配置preDispatch()方法的正确方法是什么,以便我可以告诉AuthController重定向到最初请求的页面?
您应该记住preDispatch中的当前网址,然后在您的登录操作中重定向到该网址.例:
public function preDispatch() { $requestUri = Zend_Controller_Front::getInstance()->getRequest()->getRequestUri(); $session = new Zend_Session_Namespace('lastRequest'); $session->lastRequestUri = requestUri; // ... } public function loginAction() { // ... $session = new Zend_Session_Namespace('lastRequest'); if (isset($session->lastRequestUri)) { $this->_redirect($session->lastRequestUri); return; } // ... }
您可以为此编写自己的Zend_Controller_Action_Helper.您也可以检查HTTP_REFERRER,但对我来说,会话是存储此类数据的更可靠方式.