执行控件操作后如何运行操作(在所有页面中)

发布于 2024-11-05 05:23:01 字数 1274 浏览 0 评论 0原文

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

它对于 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,哪个数字有用?

I have a plugin for frontcontroller.

It works fine for dispatcherloodstartup method, but postdispatcher can't call action!

What is wrong?

This is my plugin:

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');
    }
}

this is my bootstrap code:

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

if I must make use of stackpush, which number is useful?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〃温暖了心ぐ 2024-11-12 05:23:01

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

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

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

编辑:

如果将工具栏渲染放入布局中,则没有问题。您可以将变量分配给视图,它们将在布局中准备就绪。您只需 echo $this->render('toolbar.phtml'); 就可以了。由于布局是最后渲染的视图,因此它始终会在渲染所有操作之后渲染。只需确保您的工具栏变量不会与其他变量发生冲突(好主意是用某些东西重新修复它们 - 例如 $this->toolbarUsername;$this->toolbarIsLogged 等)

First. This is a bad practise. You have to do 3 dispatch roundabouts (all plugins pre- and post-dispatch, controllers pre&post, ...)- If possible I would switch to one front controller plugin, that will fill the appropriate layout variables and use partials/view helpers to render them.

The problem with your code is that in postDispatch() your request has already isDispatched = true, which means it won't be dispatched again. Option might be to create the request yourself. And then push it to the actionstack.

Also you can use actionstack methods $this->actionstack($action, $controller, $module, $params), that will create the request for you ;) You can get the current params like this $params = $frontController->getRequest()->getParams() or sth. like that (not 100% sure about the method name).

EDIT:

If you put the toolbar rendering into layout, then there is no problem. You can assign the variables to the view and they will be ready in layout. You'll just have echo $this->render('toolbar.phtml'); and that's all. It will always be rendered after all actions are rendered as layout is the last view rendered. Just make sure your toolbar variables don't collide with other variables (good idea is to refix them with something - say $this->toolbarUsername;$this->toolbarIsLogged, etc.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文