ZendFramework将变量从Controller发送到View(最佳实践) [英] ZendFramework Send variables from Controller to View (Best pactice)

查看:74
本文介绍了ZendFramework将变量从Controller发送到View(最佳实践)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Zend Framework中工作了一段时间,目前正在重构代码的某些部分.我要消除的最重要的事情之一是我的abstract控制器类,该类初始化了许多必须存在于我的所有控制器中的变量,例如$success$warning$error.这部分可以在控制器插件中完成,但是将这些变量发送到相关视图的最佳方法是什么.目前,我在abstract控制器类中使用了自定义方法,该方法是在所有控制器中调用的.

I have been working in Zend Framework for a while and I am currently refactoring some parts of my code. One of the big thing I would like to eliminate is my abstract controller class which initiate a lot of variables which must be present in all my controller such as $success, $warning and $error. This part can be done in controller pluggins but what would be the best way to send these variables to the related view. Currently I am using a custom method in my abstract controller class which i call from within all my controllers.

protected function sendViewData(){
    $this->view->success  = $this->success;
    $this->view->warning  = $this->warning;
    $this->view->error    = $this->error;
}

然后在我所有控制器的所有动作中调用

which is then called in all the actions of all of my controllers throught

parent::sendViewData();

我一直在寻找通过插件控制器或任何更适合于此的东西自动化该过程

I was looking to automate this process through a plugin controller or anything better suited for this

推荐答案

您可以设置 postDisplatch 方法可初始化视图数据(请参见调度前和调度后挂钩"一节).

You could set a postDisplatch method in your abstract controller to initialize the view data (See section "Pre- and Post-Dispatch Hooks").

这样,您可以在每个操作中初始化$this->success$this->warnning$this->error变量,并在执行操作后将其传递给视图.

That way, in each actions, you could initialize your $this->success, $this->warnning or $this->error variables, and it would be pass to the view after the action is executed.

这篇关于ZendFramework将变量从Controller发送到View(最佳实践)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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