tags:

views:

3688

answers:

6

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?

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

+4  A: 

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.

neilcrookes
+2  A: 

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

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.

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

Travis Leleu
A: 

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

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

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

jimiyash
A: 

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'));

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

A: 

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

cakecoder
Your link is 404...
xav0989
Yes, this link should be updated or removed.
benjamin
A: 

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

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

cdburgess