在 CakePHP 中从助手调用模型有缺点吗?
一些背景信息:我需要缓存 CakePHP 站点的主页 - 除了一小部分,它根据用户的 IP 地址显示用户本地的事件。
显然,您可以使用
nocache 标签可以有效地包围的是元素和助手。因此,我在这些标签内创建了一个元素,它调用辅助函数来访问模型并获取适当的数据。从我正在使用的助手获取模型:
$this->Modelname =& ClassRegistry::init("Modelname");
然而,在我看来,无论是从 CakePHP 还是一般 MVC 原则来看,这都是一种不确定的做事方式。所以我的问题是,这是完成我想做的事情的适当方式,还是应该敲响警钟?有没有更好的方法来实现我在这里所缺少的目标?
A bit of context: I need to cache the homepage of my CakePHP site - apart from one small part, which displays events local to the user based on their IP address.
You can obviously use the <cake:nocache> tag to dictate a part of the page that shouldn't be cached; but you can't surround a controller-set variable with these tags to make it dynamic. Once a page is cached, that's it for the controller action, as far as I know.
What you can usefully surround with the nocache tags are elements and helpers. As such, I've created an element inside these tags, which calls a helper function to access the model and get the appropriate data. To get at the model from the helper I'm using:
$this->Modelname =& ClassRegistry::init("Modelname");
This seems to me, however, to be a kind of iffy way of doing things, both in terms of CakePHP and general MVC principles. So my question is, is this an appropriate way of getting what I want to do done, or should it ring warning bells? Is there a much better way of achieving my objectives that I'm just missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将代码放入元素中并在元素内部使用 requestAction,而不是使用 Helper。
请参阅此链接
http://bakery。 cakephp.org/articles/gwoo/2007/04/12/creating-reusable-elements-with-requestaction
这将是一个比尝试使用更好的方法模型在你的助手中。
Rather than using a Helper, try to put your code in an element and use requestAction inside of the element.
see this link
http://bakery.cakephp.org/articles/gwoo/2007/04/12/creating-reusable-elements-with-requestaction
This would be a much better approach than trying to use a model in your helper.
除了打破 MVC 所有精心制定的原则之外?
除了将此项目放入元素之外,为什么不使用简单的 ajax 来获取它呢?
无需直接从视图调用模型,并且无需使用 requestAction 让事情陷入困境。 :)
HTH
Other than breaking all the carefully-laid principles of MVC?
In addition to putting this item into an element, why not fetch it with a trivial bit of ajax?
No need to muck around calling Models directly from Views, and no need to bog things down with requestAction. :)
HTH