Forward to a different action/controller before preDispatch with Zend Framework
I've something like
<?php
class AccountController extends Zend_Controller_Action
{
public function init()
{
if(!Zend_Auth::getInstance()->hasIdentity()) {
//need to forward to the auth action in another controller,
//instead of dispatching to whatever action it would be dispatched to
}
}
...
I can't use $this->_forward("action") because the auth action is not in the same controller and in the init, forwarding has to be for a action in the s开发者_Python百科ame controller. Any ideas?
$request->setModuleName('module')
->setControllerName('controller')
->setActionName('action');
also doesn't work. I've tried to clear the params, but still I've got nothing.
Put it in a Front Controller Plugin
class My_Auth_Plugin extends Zend_Controller_Plugin_Abstract
{
public function preDispatch($request)
{
if(!Zend_Auth::getInstance()->hasIdentity()) {
$request->setModuleName('module')
->setControllerName('controller')
->setActionName('action');
}
}
}
You may want to redirect instead of 'forwarding', you can use the redirector helper.
$this->_helper->redirector('action', 'controller', 'module');
Note that it's the basic usage of the redirector helper, you may also want to use goToRoute() method which allows you to use custom routes.
Here is the signature of the method:
public function gotoRoute(array $urlOptions = array(), $name = null,
$reset = false, $encode = true)
The usage is similar to the view helper url()
There are multiple parameters for _forward.
$this->_forward($action, $controller, $module, $params)
If controller is null then action will be within the called controller. If module is null, then controller will be within the called module.
param is an array of parameters. I tend to pass $this->_request->getParams()
.
精彩评论