CakePHP requestAction() 带元素的替代选项
我面临的情况是,我试图将值从控制器操作直接传递到元素。
我知道如何做到这一点的唯一方法是使用 requestAction(),但由于资源原因我不喜欢使用该功能,并且手册中不推荐使用该功能。
任何人都可以阐明另一种方法吗?
这是我现在所拥有的:
Bird Controller action:
function element_array_pass() {
$this->paginate['Bird'] = array(
'fields' => array('id', 'name'),
'contain' => array('id', 'name'),
'order' => 'Bird.id'
);
$bird_elmnt = $this->paginate('Bird');
$this->set(compact('bird_elmnt', $bird_elmnt));
}
dir:views/elements/element_array_pass.ctp
debug($bird_elmnt); // nothing being passed here.
dir: views/birds/index.ctp
我在此文件中包含该元素,但该数组不会通过。
echo $this->element('element_array_pass',array("bird_elmnt" => $bird_elmnt)); // call to element.
我想将此元素包含在我的 index.ctp 中,并使用从上面包含的 action() 传递的值。
I am in a situation where I am trying to pass a value from a controller action to an element directly.
The only way I know how to do this is with requestAction(), but I don't like using that feature for resource reasons and its not recommended in the manual.
Can anyone shed some light on another method?
Here is what I have now:
Bird Controller action:
function element_array_pass() {
$this->paginate['Bird'] = array(
'fields' => array('id', 'name'),
'contain' => array('id', 'name'),
'order' => 'Bird.id'
);
$bird_elmnt = $this->paginate('Bird');
$this->set(compact('bird_elmnt', $bird_elmnt));
}
dir: views/elements/element_array_pass.ctp
debug($bird_elmnt); // nothing being passed here.
dir: views/birds/index.ctp
I include the element in this file, but the array will not pass.
echo $this->element('element_array_pass',array("bird_elmnt" => $bird_elmnt)); // call to element.
I would like to include this element in my index.ctp with values passed from the action() I included above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过
element()
方法的第二个参数将附加数据传递给元素。另请阅读 这篇文章来自马克·斯托里。
You are able to pass additional data to an element through the 2nd argument of the
element()
method.Also have a read of this article from Mark Story.