![]() |
Controller Constants - 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: Controller Constants (/showthread.php?tid=30173) |
Controller Constants - El Forum - 05-05-2010 [eluser]BradEstey[/eluser] Every page that I call needs to run the same model first to get the page header/meta tags/section id/page template/etc from the database. Currently I'm putting the same line of code at the top of every Controller Function to call that Model (Pageload->getPage()) and return the page info for that particular page. There has got to be an automated way to do this, does anyone know where? Even if I have to call it once per Controller somehow, that would still be better than calling it for every function. Code: class Home extends Controller { Controller Constants - El Forum - 05-05-2010 [eluser]danmontgomery[/eluser] You have a couple of options. - Extend the controller class, call the code in the constructor of that base class so that it is automatically called whenever a controller extends the base class. - Put it in the constructor of each controller that needs to call it. Either way, the variable would need to be assigned to a class member, not a local variable. Code: class Home extends Controller { Controller Constants - El Forum - 05-05-2010 [eluser]BradEstey[/eluser] Worked like a charm! I knew I would probably have to call it in the constructor but I wasn't sure how to pass those variables to all of the functions. I guess I overlooked the obvious! ![]() Thanks noctrum! |