CodeIgniter Forums
Library or helper? - 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: Library or helper? (/showthread.php?tid=10017)



Library or helper? - El Forum - 07-16-2008

[eluser]bennyhill[/eluser]
I am having trouble managing my left sidebar navigation for my product catalog. The navigation is two levels deep and appears across all pages. I am currently loading a "leftnav_view" on all pages but to get this to function it requires many database queries to get what department the user is in as well as what category and product. Doesn't seem like elegant, reusable code.


Should I make a Library or a Helper Class to manage this better? Any tips on how others have handled a basic navigation?


Library or helper? - El Forum - 07-16-2008

[eluser]Bramme[/eluser]
It kinda depends: do you use multiple controllers, or only one?

I'd make my constructor look like this:
Code:
function __construct()
{
    // do all your queries here
    // use a model to interact with your database if necessary
    $array['navi'] = ''; //get the array that's needed for the navigation
    $this->load->vars($array);
}
and then you can use $navi in your leftnav_view


Library or helper? - El Forum - 07-16-2008

[eluser]Rick Jolly[/eluser]
Being a fan of modularity, I'd use a helper or library and call it from the view. That way, you can include the menu in any view without syncing with the controller. Ideally, you'd cache the menu to avoid all the queries on every request. You'd have to use a third party cache library for that.


Library or helper? - El Forum - 07-16-2008

[eluser]RaZoR LeGaCy[/eluser]
Use zend_cache for that purpose, it rocks.