CakePHP:将 $this->data 从控制器传递到视图
我正在使用 CakePHP 1.2,我只是想知道将 $this->data 从控制器传递到视图是否有任何副作用。
例如:
// inside PostsController, I have this code:
$this->data['Posts'] = $this->Post->find('all');
而不是 :
$posts = $this->Post->find('all');
$this->set(compact('posts'));
// inside the /posts/view, I access it like this:
<?php foreach ($this->data['Posts'] as $post) {....};?>
通过这样做,我从控制器中一起跳过了 $this->set() 。这是否违反了任何 MVC 模式或我可能忽略的任何安全问题?我看到使用 Auth 组件,$this->data 包含 [_Token] 数组。
谢谢
I'm using CakePHP 1.2 and I'm just wondering if there is any side affect on passing the $this->data to the View from the Controller.
Ex:
// inside PostsController, I have this code:
$this->data['Posts'] = $this->Post->find('all');
instead of :
$posts = $this->Post->find('all');
$this->set(compact('posts'));
// inside the /posts/view, I access it like this:
<?php foreach ($this->data['Posts'] as $post) {....};?>
By doing this, I skipped the $this->set() from the controller all together. Does this violate any MVC pattern or any security issue that I might have overlook? I saw that using the Auth Component, $this->data contains the [_Token] array.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要注意 Cake Helpers 自动查找数据的不同位置,因为这才是真正产生影响的地方。表单助手将根据
$this->data
的内容自动填写字段。这就是验证失败时表单数据保留的方式。 OTOH,因此,
$this->data
有它特殊的地方,不应该轻易使用,就像命名变量有其用途一样,不应该被忽视。酌情使用两者。如果您不想自动设置表单助手内容,请set()
您的变量。恕我直言,分配一个暗示其包含的数据的变量名称也更具可读性。在$this->data
上操作的所有视图都不如在$foo
上操作的一个视图和在$bar
上操作的另一个视图清晰。You need to be aware of the different places that Cake Helpers automagically look for data, since that is were it makes a real difference. The Form Helper will fill in fields automatically based on the contents of
$this->data
. That's how form data persists when validation fails. OTOH, a<select>
elements options array is automatically taken from the pluralized field name,e.g.
$form->select('Model.foo_id')
will take its options from$foos
if set.As such,
$this->data
has its special place and shouldn't be used lightly, just as named variables have their use and shouldn't be ignored. Use both as appropriate. If you don't want to auto-set Form Helper content,set()
your variables. IMHO it's also more readable to assign a variable name that hints at the data it contains. All your views operating on$this->data
is less clear than one view operating on$foo
and another on$bar
.在 CakePHP 2.x 中,如果是普通的
$this->data
,则应该使用$this->request->data
,否则您最终可能会收到此错误:In CakePHP 2.x you should use
$this->request->data
instead if plain$this->data
, otherwise you might end up getting this error:$controller->data
表示从视图文件发布到控件的数据。$view->data
用于一般数据。我会避免这样做以使自己保持理智。除此之外,您还在视图中输入更多内容。
$controller->data
is meant for data posted to the control from view file.$view->data
is for general data.I would avoid doing it to keep myself sane. besides you are typing more in view.
除了使用表单之外,没有充分的理由直接设置 $this->data 。
为什么打破约定 - Controller:set 的存在是有原因的。如果您想将数据传递到视图以用于显示或显示逻辑目的,您应该使用提供的函数,而不是尝试使用 Controller:data 来实现意想不到的目的。
如果您遵循规则并以预期的正确方式做事,那么在 CakePHP 中一切都会变得更容易。
There is no good reason for setting $this->data directly except when working with forms.
Why break convention - Controller:set is there for a reason. If you want to pass data to the view for display or display logic purposes you should use the function provided instead of trying to co-opt Controller:data for unintended purposes.
Everything is easier from within CakePHP if you follow the rules and do things the expected, correct way.
在
cakephp
版本 2.* 中,当您尝试在$this->data
上设置数据时会发生错误In
cakephp
version 2.*, error occurs when you try to set data on$this->data