Welcome Guest, Not a member yet? Register   Sign In
Where should I put menu generation code?
#1

[eluser]tmcw[/eluser]
Okay, so working on le CMS... in my public controllers, for each page generated, I've been putting a bit of logic for fetching items from a Page_model, sorting those into a tree, and putting that on a page. I've got it to the point that I at least have that logic in a function within each controller, so I can call it like $data['menu'] = $this->menu('Page_model') and so on.

My question is whether there's a better way to do this... as a helper, plugin, etc? I've been running into the problem that helpers seem to exist outside of the CI object system, so I can't call in a model. Plugins, I think, can, but the manual tells me that they usually have only one associated function. I don't know why.

Any suggestions? It isn't a killer problem, but I figure it's fairly common and I'd like to build this thing the best way I can.

Thanks
#2

[eluser]tmcw[/eluser]
-sorry, posted a follow-up but it turned out to be wrong.
#3

[eluser]obiron2[/eluser]
personally, I would put the logic that generates the menu data into a model and the code that generates the menu HTML into a view.

you will need to call the menu model in the controller to pass the data to the view.

This way, if you change the way that the menu data is stored (e.g. in a database rather than as an array) you don't have to change everything.
#4

[eluser]Michael Wales[/eluser]
I agree with obiron - create a model to handle all of your dynamically generated content areas that are global throughout the site (header, navigation, footer, whatever). Call it from the controller (or setup a hook to do it across every controller), and push it out to the view.
#5

[eluser]Phil Sturgeon[/eluser]
[quote author="tmcw" date="1187672092"]I've been running into the problem that helpers seem to exist outside of the CI object system, so I can't call in a model. Plugins, I think, can, but the manual tells me that they usually have only one associated function. I don't know why.[/quote]

Don't forget you can grab a CI instance from any PHP file which is loaded in the same instance.

Code:
// PHP 4
$CI =& get_instance();

//PHP 5
$CI = get_instance();

$CI->load->model('page_model');




Theme © iAndrew 2016 - Forum software by © MyBB