I'm trying to centralise my redirects (based on authentication and various other states) into a front controller plugin. So far I've tried:
$this->setRequest(new Zend_Controller_Request_Http('my_url'));
at various points in the plugin (i.e. from routeStartup to dispatchLoopShutdown) and also:
$this->setResponse(new Zend_Controller_Response_Http('my_url'));
Can anyone offer some assistance on this, or point me in the direction of a tutorial?
The easiest way would be to use ZF's Redirect ActionHelper
$r = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
$r->gotoUrl('/some/url')->redirectAndExit();
Alternatively instantiate it without the HelperBroker
$r = new Zend_Controller_Action_Helper_Redirector;
$r->gotoUrl('/some/url')->redirectAndExit();
The ActionHelper provides an API solely concerned about redirecting through a number of methods, like gotoRoute
, gotoUrl
, gotoSimple
, which you can use depending on your desired UseCase.
Internally, the ActionHelper uses the APIs of Response and Router to do the redirect though, so you can also use their methods directly, e.g.
$request->setModuleName('someModule')
->setControllerName('someController')
->setActionName('someAction');
or
$response->setRedirect('/some/url', 200);
Further reading: