cakePHP布局数据,使用requestAction,还有其他方法吗?
这是一个已知问题,requestAction 是一种资源浪费。但是,我的默认布局中需要一些动态数据。我正在使用元素,通过 requestAction 调用。
这是唯一的方法吗?
I'ts a known issue, requestAction is a resource waste. however, I need some dynamic data in my default layout. I'm using elements for it, with requestAction calls.
Is it the only way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
requestAction 没有任何问题,只是很容易低效地调用它并影响应用程序的性能。按照它的工作方式,你最终会得到充满属性的怪物物体,等等——你不需要的东西,如果你意识到它会这样发生,可能就不会拉。有多少开发人员不会多花三秒钟来输入 $this->Other->Model->$它是否使您免于尝试调试这些庞然大物之一?
100 次中有 85 次,有一种更有效(应用程序性能)的方法来完成您需要 requestAction() 做的任何事情。实际上,蛋糕中内置了另一种方法,它几乎可以做同样的事情,只是它是在控制器中使用的,如果我能找到它,我会链接...
编辑我似乎无法找到它。这是一个有点额外晦涩难懂的东西。
回答/建议您的问题,在视图/元素中避免 requestAction 特别容易。
如果数据不那么抽象,更动态,ajax->调用您的控制器/方法,传递任何数据等,获取动态结果。
如果数据高度抽象或适用 - 因此您在默认布局中需要它 - 考虑将其塞入您的 AppController 甚至引导加载程序中。这可能是一个更有效的解决方案 - 将其从处理阶段推出,而不是在渲染阶段向上游推进以获取它。
当然,组件/助手将是定位多控制器+布局级视图数据获取的另一种方式。
哈:)
There's nothing wrong with requestAction, it's just very easy to invoke it inefficiently and skewer your app's performance. The way it works, you'll end up with monster objects bulging with properties, etc - stuff you don't need and probably wouldn't have pulled if you'd realized it was going to happen like that. How many devs wouldn't spend three extra seconds to type $this->Other->Model-> if it saved you from potentially trying to debug one of those behemoth objects?
85 times out of 100, there's a more efficient (app performance) way to do whatever you needed requestAction() to do. There's actually another method built into cake that does almost the same thing, only it's meant to be used in controllers and if I can find it, I'll link...
Edit I can't seem to find it. It's something a little extra obscure.
Answer / suggestion to your question, avoiding requestAction is especially easy in views / elements.
If the data is less abstract and more dynamic, ajax -> call your controller / method, pass any data, etc, fetch your dynamic result.
If the data is highly abstract or applicable - such that you need it in your default layout - consider tucking it into your AppController or even the bootloader. This is probably a much more efficient solution - pushing it from the process stage rather than paddling upstream to get it at the rendering stage.
Components / helpers, of course, would be another way to position multiple-controller + layout-level view data fetching.
HTH :)
我相信 requestAction 是一个很好的工具。大多数 MVC 框架甚至没有考虑显示多个内容的需要,而大多数情况下您确实需要在单个布局中显示多个内容。
我不确定它如何浪费资源以及为什么文档让您相信它是性能瓶颈。使用正确的键进行缓存,应该可以解决问题。
I believe requestAction is a good tool. Most MVC frameworks do not even think of the need to show multiple pieces of content, whereby you do need that most of the cases within a single layout.
I'm not sure how it wastes resources and why the documentation makes you believe it is a performance bottleneck. Use caching with the right keys and it should do the trick.