在 CakePHP 中从助手调用模型有缺点吗?

发布于 2024-11-17 05:16:22 字数 472 浏览 1 评论 0原文

一些背景信息:我需要缓存 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

溺渁∝ 2024-11-24 05:16:22

尝试将代码放入元素中并在元素内部使用 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.

日久见人心 2024-11-24 05:16:22

除了打破 MVC 所有精心制定的原则之外?

除了将此项目放入元素之外,为什么不使用简单的 ajax 来获取它呢?

  1. 将调用放入其自己的控制器操作中,以便目标 URL -> /controller/action (非常方便!)
  2. 将 IP 传递回该操作,以便在查找调用中使用
  3. 将 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?

  1. Put the call in its own controller action, such that the destination URL -> /controller/action (quite convenient!)
  2. Pass the IP back to that action for use in the find call
  3. Set the ajax update callback to target within the element with the results of the call accordingly

No need to muck around calling Models directly from Views, and no need to bog things down with requestAction. :)

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文