1

我有一个前端控制器插件。

它适用于 dispatcherloodstartup 方法,但 postdispatcher 不能调用动作!

怎么了?

这是我的插件:

class Places_Controller_Plugin_ActionSetup extends Zend_Controller_Plugin_Abstract
{

public function dispatchLoopStartup( Zend_Controller_Request_Abstract $request)
    {
        $front = Zend_Controller_Front::getInstance();
        
        if (!$front->hasPlugin('Zend_Controller_Plugin_ActionStack')) {
                $actionStack = new Zend_Controller_Plugin_ActionStack();
                $front->registerPlugin($actionStack, 97);
            } else {
            $actionStack = $front->getPlugin('Zend_Controller_Plugin_ActionStack');
        }
        
            $menuAction = clone($request);
            $menuAction->setActionName('menu')->setControllerName('index');
            $actionStack->pushStack($menuAction);
        
    }
     

    public function postDispatch(Zend_Controller_Request_Abstract $request)
    {     $menuAction = clone($request);
        $menuAction->setActionName('toolbar')->setControllerName('index');
    }
}

这是我的引导代码:

$frontController->registerPlugin(new Places_Controller_Plugin_ActionSetup(), 98);

如果我必须使用stackpush,哪个数字有用?

4

1 回答 1

1

第一的。这是一个不好的做法。你必须做 3 个调度回旋处(所有插件调度前和调度后,控制器前后,...) - 如果可能的话,我会切换到一个前端控制器插件,它将填充适当的布局变量并使用部分/视图助手渲染它们。

您的代码的问题是在 postDispatch() 您的请求已经isDispatched = true,这意味着它不会再次被分派。选项可能是自己创建请求。然后将其推送到操作堆栈。

您也可以使用 actionstack 方法$this->actionstack($action, $controller, $module, $params),这将为您创建请求;)您可以像这样$params = $frontController->getRequest()->getParams()或 sth 获取当前参数。像那样(不是 100% 确定方法名称)。

编辑:

如果你把工具栏渲染放到布局中,那是没有问题的。您可以将变量分配给视图,它们将在布局中准备就绪。你只会有echo $this->render('toolbar.phtml');,仅此而已。它总是在所有动作都被渲染后被渲染,因为布局是最后渲染的视图。只要确保您的工具栏变量不会与其他变量发生冲突(好主意是用一些东西重新修复它们 - 比如说$this->toolbarUsername;$this->toolbarIsLogged,等等)

于 2011-05-05T14:00:14.720 回答