Zend:操作助手的 init 方法是否将变量传递给布局视图?
问题:我无法从操作助手的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当你的助手的 init() 被调用时,ViewRenderer 的 init() 还没有被调用。这是因为助手堆栈中的顺序。
如果启用严格的标准错误报告,您应该在帮助程序中看到类似的内容“从...中的空值创建默认对象”
您应该考虑将代码移动到 preDispatch() 挂钩,因为 init() 方法应该用于 <助手初始化。
获取控制器的视图实例:
如果要将参数传递给布局,则使用
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:
if you want to pass parameter to layout, then use
之所以如此称呼操作助手,是因为它们是避免操作中代码重复的一种方法。您在操作助手中所做的所有操作都可用于操作,但不可用于视图。这是动作的正常行为,只要您不将某些内容传递给视图,视图就不会知道。如果您想避免视图中的代码重复,请创建视图助手。
有时创建一个动作助手和相应的视图助手是有意义的。
--
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.