views:

384

answers:

2

I am working on a authentication class with Zend Framework. I added a predispatch function in Zend_Controller_Plugin_Abstract to check the user login status.

public function preDispatch(Zend_Controller_Request_Abstract $request) {
if (!$logined){
 $request->setControllerName('auth')->setActionName('login');

}
}

if user is not logined they do get diverted to the login page, however the url still shown the other url that user clicked.I want to handle everything in this predispatch plugin rather than doing redirect in every other actions/controlls pages. Any solution?

Thank you.

+1  A: 

Manipulating the controller and action once the script has started only impacts internal routing.

To change the URL, you'll need to fire an actual redirect instead.

Charles
good answer. +1
RageZ
Thanks a lot. But how do you fire a actual redirect inside the predispatch plugin?
Dan
@Dan: See Rob's (correct) answer. Things have changed quite a bit since I last used ZF, and couldn't remember off the top of my head.
Charles
+1  A: 

You have to redirect to get the address bar in a browser to change. I do this in my preDispatch() :

$request = $this->getActionController()->getRequest();
$urlOptions = array('controller' => 'auth', 
                    'action' => 'login',
                    'backTo' => $request->getRequestUri(),
                    );
$redirector = new Zend_Controller_Action_Helper_Redirector();
$redirector->gotoRouteAndExit($urlOptions, null, true);
Rob Allen