在使用Zend Framework进行preDispatch之前转发到不同的操作/控制器

Hen*_*nte 1 model-view-controller routing zend-framework controller dispatcher

我有类似的东西

<?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
        }
    }
...
Run Code Online (Sandbox Code Playgroud)

我不能使用$ this - > _ forward("action"),因为auth操作不在同一个控制器中,而在init中,转发必须是在同一个控制器中的动作.有任何想法吗?

$request->setModuleName('module')
            ->setControllerName('controller')
            ->setActionName('action');
Run Code Online (Sandbox Code Playgroud)

也行不通.我试图清除这些参数,但我仍然没有.

Bor*_*éry 5

把它放在Front Controller插件中

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');
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

您可能想要重定向而不是"转发",您可以使用重定向器帮助程序.

$this->_helper->redirector('action', 'controller', 'module');
Run Code Online (Sandbox Code Playgroud)

请注意,这是重定向器帮助程序的基本用法,您可能还希望使用goToRoute()方法,该方法允许您使用自定义路由.
这是方法的签名:

public function gotoRoute(array $urlOptions = array(), $name = null, 
    $reset = false, $encode = true)
Run Code Online (Sandbox Code Playgroud)

用法类似于视图助手 url()