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
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With