CakePHP中的动态布局 [英] Dynamic layouts in CakePHP
问题描述
对于问题标题很抱歉,但我找不到更适当的方式来表达这个问题。
Sorry about the question title, but I couldn't find a more appropriate way to phrase this.
我目前正在建立一个CakePHP网站,不太确定如何解决以下问题。网站看起来像下面的模型:
I am currently building a CakePHP powered website and I'm not quite sure how to approach the following issue. The website looks something like the follwing mockup:
。
灰色区域是布局的一部分,因为它们的内容在视图之间不会改变。在侧栏中,我有一组与多个模型相关联的广告。我需要控制器逻辑来确定与广告相关联的图片。此外,广告列表需要是动态的。我应该把构建边栏的逻辑放在哪里?
The greyed out areas are part of the layout, because their content does not change between views. In the sidebar, I have a collection of ads who are linked to several models. I need controller logic to determine the picture associated with an ad. Also, the ad list needs to be dynamic. Where should I put the logic for building the sidebar?
我想过:
- 将逻辑放入
AppController
(beforeFilter
/afterFilter
) - 问题是, t使用控制器逻辑我需要(其他控制器继承自AppController
,我不知道如何使用它们)。 - 在渲染视图的所有控制器中复制侧边栏代码 - 这似乎对我很蠢。
- putting the logic into the
AppController
(beforeFilter
/afterFilter
) - the problem is I can't use the controller logic I need (the other controllers inherit fromAppController
, I'm not sure how to use them there). - making a component - is it okay to build components that rely on controllers?
- replicating the sidebar code in all controllers that render views - this seems kind of stupid to me.
经过一些阅读和实验,我已经重构了大部分内容。
After some reading and experimenting, I've gotten to refactoring most of it.
通过移动在模型中构建我的广告的逻辑(删除检索图片的组件),而不是使用 requestAction
,我获得了最佳性能。它几乎快了三倍,代码看起来好多了。
I obtained the best performance by moving the logic for building my ads in the model (eliminating the component that retrieved the pictures) and not using requestAction
. It's almost three times faster and the code looks much better.
推荐答案
我做了类似的数据驱动导航。我把我的逻辑在 AppController :: beforeRender
,没有任何问题。我不知道我理解你关心的控制器继承的关注。我通过以下方式检索我的菜单:
I've done something similar for data-driven navigation. I put my logic in AppController::beforeRender
and haven't had any problems. I'm not sure I understand your concern related to controller inheritance. I retrieve my menus via:
$menus = $this->NavMenuItem->groupByMenu();
$this->set( compact( 'menus' ) );
然后我创建了一个渲染菜单的元素。它由布局执行:
I then created an element that renders the menu. It's executed by the layout via:
<?php echo $this->element( 'navigation', array( 'id' => 'secondary', 'menu' => $menus['SECONDARY'] ) ) ?>
如果这没有帮助,也许你可以在注释中进一步解释控制器继承的问题。
If that doesn't help, maybe you can further explain your issue with controller inheritance in a comment.
这篇关于CakePHP中的动态布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!