Reputation: 5342
class IndexController extends Zend_Controller_Action{
public function init(){
if(!isset($_SESSION['administrator'])){
$this->_helper->redirector->gotoRouteAndExit(array('controller' => 'index', 'module' => 'cp', 'action' => 'login'), null,!0,!0);
}
}
}
I tried on Firefox, it shows me:
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
But when I put redirector outside of init() method, it works.
public function indexAction(){
if(!isset($_SESSION['administrator'])){
$this->_helper->redirector->gotoRouteAndExit(array('controller' => 'index', 'module' => 'cp', 'action' => 'login'), null,!0,!0);
}
}
Can anyone help me?
Edit:
I don't want to repeat the "redirect code" because there are many other actions need to be checked permission in the same controller.
Upvotes: 0
Views: 878
Reputation: 1141
The problem is, that on index/login
, you also run the init()
method, detect that the user is not an admin, and try to again redirect him to index/login
, which will run the init()
method again, detect that he still is not an admin... and so on. Thus the infinite redirect loop.
So in your init()
you may first check if you are trying to call the login
action, and only if not, check for admin permissions and redirect to index/login
.
In your concrete case, you may want to change your init()
method as follows:
public function init(){
if ($this->getRequest()->getActionName() !== 'login' && !isset($_SESSION['administrator'])){
$this->_helper->redirector->gotoRouteAndExit(array('controller' => 'index', 'module' => 'cp', 'action' => 'login'), null,!0,!0);
}
}
This excludes the login
action from checking admin permissions and redirecting, breaking the infinite redirect loop.
Upvotes: 2
Reputation: 1567
The thing is, init()
function will always runs first in any controller,
So When you do a redirect is init()
it essential redirects to itself , which will create infinite loop in your case, so it will keep redirecting to itself, as "BluePsyduck" Suggested.
So do the alternate what you have already done and redirect it in index Action
of your controller..
Upvotes: 1