CodeIgniter Forums
How Do I Create a Menu based on Controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How Do I Create a Menu based on Controller (/showthread.php?tid=41818)



How Do I Create a Menu based on Controller - El Forum - 05-17-2011

[eluser]atolar[/eluser]
How do people usually create menus for CodeIgniter projects? (I'm new).

What I want:
<b>In each of my controllers that I want to be a part of the menu, create a "$menuItems" variable that has the specific items for that controller. When each page is loaded, I need to get the values of all of those $menuItems from each Controller to compile the items that should be in the menu.</b> I've tried the get_class_vars('<controller-name>') but I seem to not have the scope to see vars in other controllers.

Every menu tutorial I've seen uses a database but that is definitely not the way I want to do it. I want to do this in the code and keeping it separate for each controller.


<b>Thanks</b> for your help!


How Do I Create a Menu based on Controller - El Forum - 05-17-2011

[eluser]oppenheimer[/eluser]
I don't know of a way to access variables from controllers that aren't active. However, why not try it the other way around. Read this article from Phil Sturgeon. Then put your menu data into My_controller.


How Do I Create a Menu based on Controller - El Forum - 05-17-2011

[eluser]atolar[/eluser]
So that means I would have to add the menu information to the controller? I could just as easily create a helper method to do that all. However, this would make my code less organized and harder to maintain.


How Do I Create a Menu based on Controller - El Forum - 05-19-2011

[eluser]atolar[/eluser]
I've found a solution that works for me.

Basically, I've placed the menu specifications in yaml files in a new APPPATH.'menus' folder.

I then use the "Yaml with Symphony yaml" library to load my specifications into an array and then loop through, adding each item to the appropriate menu.

If anybody is interested, I'm willing to offer my code, just let me know.