Zend:操作助手的 init 方法是否将变量传递给布局视图?

发布于 2024-11-09 09:27:22 字数 990 浏览 0 评论 0原文

问题:我无法从操作助手的 init 方法将变量传递到自定义布局。

我有这个动作助手“My_Action_Helper_Initializer”:

class My_Action_Helper_Initializer extends Zend_Controller_Action_Helper_Abstract
{
 public function init()
 {
  $controller=$this->getActionController();
  //variable passed to controller's view
  $controller->view->flop="FLOOP!!";
  //variable passed to controller
  $controller->boom="BOOM!!";
 }
}

在我的控制器“IndexController”中:

class IndexController extends Zend_Controller_Action
{ 
  public function indexAction()
  {
    //print the variable passed from action helper
    echo $this->boom;
  }
}   

然后在我的“layout.phtml”中:

//print variable passed from action helper
echo $this->flop;
  • 因此控制器操作回显的“boom”变量正确显示。

  • 未显示“flop”变量(传递到我的布局)。

    问题:为什么传递给控制器​​操作的变量能够正确输出,而传递给布局视图的另一个变量却不能正确输出?

谢谢卢卡

PROBLEM: I can't pass my variables to my custom layout from action helper's init method.

I have this action helper "My_Action_Helper_Initializer":

class My_Action_Helper_Initializer extends Zend_Controller_Action_Helper_Abstract
{
 public function init()
 {
  $controller=$this->getActionController();
  //variable passed to controller's view
  $controller->view->flop="FLOOP!!";
  //variable passed to controller
  $controller->boom="BOOM!!";
 }
}

In my controller "IndexController":

class IndexController extends Zend_Controller_Action
{ 
  public function indexAction()
  {
    //print the variable passed from action helper
    echo $this->boom;
  }
}   

then in my "layout.phtml":

//print variable passed from action helper
echo $this->flop;
  • So the "boom" variable echoed out by controller action is shown correctly.

  • The "flop" variable (passed to my layout) is not shown.

    QUESTION: Why the variable passed to controller action is correctly output while the other one passed to the layout view is not?

Thanks

Luca

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

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

发布评论

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

评论(2

叹倦 2024-11-16 09:27:22

当你的助手的 init() 被调用时,ViewRenderer 的 init() 还没有被调用。这是因为助手堆栈中的顺序。

如果启用严格的标准错误报告,您应该在帮助程序中看到类似的内容“从...中的空值创建默认对象”

您应该考虑将代码移动到 preDispatch() 挂钩,因为 init() 方法应该用于 <助手初始化。

获取控制器的视图实例:

function getView()
{
    $controller = $this->getActionController();
    if($view = $controller->view) {
        return $view;
    }

    if($this->getFrontController()->getParam('noViewRenderer') {
        return $controller->initView();//this view instance will not be used in Zend_Layout!
    }
    $vr = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
    return $vr->initView();      

}

如果要将参数传递给布局,则使用

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

When your helper's init() called, ViewRenderer's init() wasn't yet. This is because of order in helpers stack.

If you enable strict standards error reporting, you should see something like this in your helper "Creating default object from empty value in ..."

You should consider moving your code to preDispatch() hook as init() method should be used for helper initialization.

To get view instance for controller:

function getView()
{
    $controller = $this->getActionController();
    if($view = $controller->view) {
        return $view;
    }

    if($this->getFrontController()->getParam('noViewRenderer') {
        return $controller->initView();//this view instance will not be used in Zend_Layout!
    }
    $vr = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
    return $vr->initView();      

}

if you want to pass parameter to layout, then use

$view = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->initView();
苍景流年 2024-11-16 09:27:22

之所以如此称呼操作助手,是因为它们是避免操作中代码重复的一种方法。您在操作助手中所做的所有操作都可用于操作,但不可用于视图。这是动作的正常行为,只要您不将某些内容传递给视图,视图就不会知道。如果您想避免视图中的代码重复,请创建视图助手。

有时创建一个动作助手和相应的视图助手是有意义的。

--

preDispatch() 有效,而 init() 无效,因为使用 init 时,您实际上并没有挂钩到调度过程。

Action helpers are called so because they are a way to avoid code duplication in actions. Everything you do in an action helper is available to the actions but not to the view. That's the normal behaviour of actions, as long as you don't pass something to the view, the view doesn't know it. If you want to avoid code duplication in your views, create view helpers.

Sometimes it can make sense to create an action helper and a corresponding view helper.

--

preDispatch works() while init() doesn't because with init you're not actually hooking into the dispatch process.

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