在CakePHP中创建动态数据库驱动菜单的最佳方法是什么?

时间:2022-09-15 19:51:29

I want to display a menu on every page that is run from a database. Using just down and dirty php thats easy but I want to integrate it with cakephp using their MVC system. Now my question is how is the best way to go about this?

我想在从数据库运行的每个页面上显示一个菜单。使用简单的下来和脏的PHP很简单但我想使用他们的MVC系统将它与cakephp集成。现在我的问题是如何最好的方式来解决这个问题?

My thoughts are to make an element with the layout and then a component or controller for all the logic. Any Suggestions on this? Or is a Helper what i want to use?

我的想法是用布局创建一个元素,然后为所有逻辑创建一个组件或控制器。对此有何建议?或者是我想要使用的助手?

I also need to get all the data from multiple tables in the database. Is it best to do all my data collecting logic through one model? Or do most of it in the menu controller (or component) and use the models for each table?

我还需要从数据库中的多个表中获取所有数据。是否最好通过一个模型完成所有数据采集逻辑?或者在菜单控制器(或组件)中执行大部分操作并使用每个表的模型?

Thank you,

Walter

6 个解决方案

#1


Models should fetch and process data from the table that they are modelising, so fetch menu data for each model in that model.

模型应该从他们正在建模的表中获取和处理数据,因此获取该模型中每个模型的菜单数据。

Components are intended to house logic shared by multiple controllers, so a menu component that is used by all your controllers sounds like a good place to put the code to fetch the menu data from the models and mash it together.

组件用于容纳由多个控制器共享的逻辑,因此所有控制器使用的菜单组件听起来像是一个放置代码以从模型中获取菜单数据并将其混合在一起的好地方。

A menu is typically a nested list, if this is the case with your menu too, the easiest way to output the markup for this is a recursive function (a function that calls itself) which outputs one level at a time, so rather than an element, I'd just create a helper with a menu() method in there, and call that directly from the layout.

菜单通常是一个嵌套列表,如果你的菜单也是如此,输出标记的最简单方法是递归函数(一个自己调用的函数),它一次输出一个级别,而不是一个我只需要在其中创建一个带有menu()方法的助手,然后直接从布局中调用它。

#2


I agree with neilcrooks answer, but would like to add a few things for clarity.

我同意neilcrooks的回答,但是为了清楚起见,我想补充一些内容。

Helpers and elements are pretty simple, except that helpers can be a little more robust (at least, that's how I think of 'em ;) ). So, if you use a helper, you can bind and unbind model associations on the fly -- which will allow you to grab data from your (temporarily) associated models.

帮助者和元素非常简单,除了帮助者可以更健壮一点(至少,这就是我对他们的看法;))。因此,如果您使用帮助程序,您可以动态绑定和取消绑定模型关联 - 这将允许您从(临时)关联模型中获取数据。

Personally, I like fat models and skinny controllers, so I think if I were in this situation, I would use a helper and temporarily bind the models to it. Any data that I need to fetch from the existing models would be accessed through Model1->fetchMenuPart(...) type calls.

就个人而言,我喜欢胖模型和瘦调控制​​器,所以我想如果我遇到这种情况,我会使用帮助器暂时将模型绑定到它。我需要从现有模型中获取的任何数据都可以通过Model1-> fetchMenuPart(...)类型调用来访问。

Then you can call your helper from your layout file(s).

然后,您可以从布局文件中调用助手。

#3


I don't know why this is not documented anywhere, but I just found this last night. The variables for your layout or elements have to be defined with the ending _for_layout. For example: $this->set('categories_for_layout',$this->Category->find('all'));

我不知道为什么在任何地方都没有记录,但我昨晚才发现这一点。必须使用结尾_for_layout定义布局或元素的变量。例如:$ this-> set('categories_for_layout',$ this-> Category-> find('all'));

I used the beforeFilter method in the AppController class, because I needed that menu in every page.

我在AppController类中使用了beforeFilter方法,因为我在每个页面都需要该菜单。

#4


Here is a great solution I discovered while searching for this very thing on the internet.

这是我在互联网上搜索这个东西时发现的一个很好的解决方案。

http://articles.classoutfit.com/cakephp-dynamic-navigation-bars/

#5


I think you would use create an element that would hold the html for the menu and then render the menu in your layout.

我想你会使用创建一个元素来保存菜单的html,然后在你的布局中渲染菜单。

echo $this->element('your menu');

To make it dynamic you set the menu links, maybe as an array in the controller.

要使其动态化,您可以设置菜单链接,也可以作为控制器中的数组。

#6


i have found one good article here http://www.milestree.com/webdev/cakephp/dynamic_menu

我在http://www.milestree.com/webdev/cakephp/dynamic_menu找到了一篇好文章

#1


Models should fetch and process data from the table that they are modelising, so fetch menu data for each model in that model.

模型应该从他们正在建模的表中获取和处理数据,因此获取该模型中每个模型的菜单数据。

Components are intended to house logic shared by multiple controllers, so a menu component that is used by all your controllers sounds like a good place to put the code to fetch the menu data from the models and mash it together.

组件用于容纳由多个控制器共享的逻辑,因此所有控制器使用的菜单组件听起来像是一个放置代码以从模型中获取菜单数据并将其混合在一起的好地方。

A menu is typically a nested list, if this is the case with your menu too, the easiest way to output the markup for this is a recursive function (a function that calls itself) which outputs one level at a time, so rather than an element, I'd just create a helper with a menu() method in there, and call that directly from the layout.

菜单通常是一个嵌套列表,如果你的菜单也是如此,输出标记的最简单方法是递归函数(一个自己调用的函数),它一次输出一个级别,而不是一个我只需要在其中创建一个带有menu()方法的助手,然后直接从布局中调用它。

#2


I agree with neilcrooks answer, but would like to add a few things for clarity.

我同意neilcrooks的回答,但是为了清楚起见,我想补充一些内容。

Helpers and elements are pretty simple, except that helpers can be a little more robust (at least, that's how I think of 'em ;) ). So, if you use a helper, you can bind and unbind model associations on the fly -- which will allow you to grab data from your (temporarily) associated models.

帮助者和元素非常简单,除了帮助者可以更健壮一点(至少,这就是我对他们的看法;))。因此,如果您使用帮助程序,您可以动态绑定和取消绑定模型关联 - 这将允许您从(临时)关联模型中获取数据。

Personally, I like fat models and skinny controllers, so I think if I were in this situation, I would use a helper and temporarily bind the models to it. Any data that I need to fetch from the existing models would be accessed through Model1->fetchMenuPart(...) type calls.

就个人而言,我喜欢胖模型和瘦调控制​​器,所以我想如果我遇到这种情况,我会使用帮助器暂时将模型绑定到它。我需要从现有模型中获取的任何数据都可以通过Model1-> fetchMenuPart(...)类型调用来访问。

Then you can call your helper from your layout file(s).

然后,您可以从布局文件中调用助手。

#3


I don't know why this is not documented anywhere, but I just found this last night. The variables for your layout or elements have to be defined with the ending _for_layout. For example: $this->set('categories_for_layout',$this->Category->find('all'));

我不知道为什么在任何地方都没有记录,但我昨晚才发现这一点。必须使用结尾_for_layout定义布局或元素的变量。例如:$ this-> set('categories_for_layout',$ this-> Category-> find('all'));

I used the beforeFilter method in the AppController class, because I needed that menu in every page.

我在AppController类中使用了beforeFilter方法,因为我在每个页面都需要该菜单。

#4


Here is a great solution I discovered while searching for this very thing on the internet.

这是我在互联网上搜索这个东西时发现的一个很好的解决方案。

http://articles.classoutfit.com/cakephp-dynamic-navigation-bars/

#5


I think you would use create an element that would hold the html for the menu and then render the menu in your layout.

我想你会使用创建一个元素来保存菜单的html,然后在你的布局中渲染菜单。

echo $this->element('your menu');

To make it dynamic you set the menu links, maybe as an array in the controller.

要使其动态化,您可以设置菜单链接,也可以作为控制器中的数组。

#6


i have found one good article here http://www.milestree.com/webdev/cakephp/dynamic_menu

我在http://www.milestree.com/webdev/cakephp/dynamic_menu找到了一篇好文章