从 postDispatch() 传递一个变量以查看 Zend Framework 中的实例 [英] Pass a variable from postDispatch() to view instance in Zend Framework

查看:25
本文介绍了从 postDispatch() 传递一个变量以查看 Zend Framework 中的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 postDispatch() 钩子的控制器插件,还有一个 $variable.

I have a controller plugin with postDispatch() hook, and there I have a $variable.

如何将这个变量传递给 view 实例?

How to pass this variable to the view instance?

我尝试了 Zend_Layout::getMvcInstance()->getView(),但这会返回新的视图实例(不是应用程序资源).与 $bootstrap->getResource('view') 相同.

I tried Zend_Layout::getMvcInstance()->getView(), but this returns new view instance (not the application resource). The same with $bootstrap->getResource('view').

我不想将它作为请求参数传递.
现在,作为一种解决方法,我使用 Zend_Registry.

I don't want to pass it as a request param.
Now, as a workaround I do it using Zend_Registry.

但是,这是最好的方法吗?

But, is it the best way?

推荐答案

我一直在使用 ViewRenderer 操作助手在需要时获取视图.这似乎是 Zend 类访问视图对象的最常见方式.

I've been using the ViewRenderer action helper to get the view when I need it. It seems to be the most common way that Zend classes access the view object.

所以,在控制器插件中:

class App_Controller_Plugin_ViewSetup extends Zend_Controller_Plugin_Abstract {

  public function postDispatch() {

    $view = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->view;

    echo $view->variable;

    $view->variable = 'Hello, World';

  }

}

在控制器中:

class IndexController extends Zend_Controller_Action {

  public function indexAction() {

    $this->view->variable = 'Go away, World';

  }

}

在视图脚本中:

<?php echo $this->variable; ?>

输出是:走开,世界走,世界

问题似乎在于视图脚本在 postDispatch() 方法被调用之前呈现,因为这确实返回了主视图对象.

It seems like the problem is that the view script renders before the postDispatch() method is called, because this does return the main view object.

这篇关于从 postDispatch() 传递一个变量以查看 Zend Framework 中的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆