问题
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?
回答1:
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 :)
回答2:
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.
来源:https://stackoverflow.com/questions/5455438/cakephp-layout-data-using-requestaction-is-there-another-way