zend _forward in controller plugin
I have a controller plugin that is detecting if the site has been set to maintenance, if it has, i want it to _forward to the maintenance controller to display the 'sorry...' mes开发者_如何学JAVAsage.
I don't want to use a redirect as this will change the current url the user is on, but _forward is a Zend_Controller_Action protected method so can't be called out of context, how do I do this?
When the preDispatch
method of your plugin is called, the request isn't dispatched. So you can "forward" just by setting the controller and action on the request:
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
if ($this->isMaintenanceMode()) {
$request->setControllerName('error');
$request->setActionName('maintenance');
}
}
Since the plugin cannot _forward() properly; In your controller(s) init()
method you can check for a flag that says your site is in maintenance mode AND the action isn't your maintenance action then $this->_forward('maintenance-action');
Something like:
// .. in your action controller
public function init() {
$maintenanceMode = checkTheFlagYourPluginSet();
// .. other code removed omitted
if ($maintainenceMode && $this->_request->getActionName() != 'maintenance-action') {
// return so that nothing else gets executed in the init()
return $this->_forward('maintenance-action');
}
}
精彩评论