best way to do this? - 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: best way to do this? (/showthread.php?tid=40482) |
best way to do this? - El Forum - 04-10-2011 [eluser]Wondering Coder[/eluser] hi everyone., I'm building an application using CI and using this structure most of my controllers: Code: function index(){ As you can see I've been using over and over the load->vars and load->view('template') in most of my controller? What's the best way to have this global or have this available in all of my controller? best way to do this? - El Forum - 04-10-2011 [eluser]shadow player[/eluser] Add them to autoload: http://ellislab.com/codeigniter/user-guide/general/autoloader.html (write a small helper that loads your desired views and vars and add it to the autoload) hope it works, and hope I helped you out =) best way to do this? - El Forum - 04-10-2011 [eluser]toopay[/eluser] use 'pre_controller' and 'post_controller' hooks. enable hooks in 'application/config' Code: $config['enable_hooks'] = TRUE; Code: $hook = array( Code: <? defined('BASEPATH') or die('No direct script access allowed.'); best way to do this? - El Forum - 04-10-2011 [eluser]toopay[/eluser] DOH!, forget there are not autocomplete here. Fix the opening tags on Bootstrap class, should be Code: <?php defined('BASEPATH') or die('No direct script access allowed.'); best way to do this? - El Forum - 04-10-2011 [eluser]wiredesignz[/eluser] @toopay, The $CI instance does not exist during the call to the pre-controller hook. That's why it is called pre-controller. @Wondering Coder, Your best option is to use the MY_Controller base class to contain all of your common code. Extend each page controller from the MY_Controller class. best way to do this? - El Forum - 04-10-2011 [eluser]toopay[/eluser] @wiredesignz, In my ilustration,I put $CI instances in construct function (not 'pre_controller') and use it during the call to 'post_controller' hook. It's a valid call. best way to do this? - El Forum - 04-11-2011 [eluser]Wondering Coder[/eluser] hi, thanks for helping me out of this one. I like the idea of extending My_Controller, in fact I'm using one.: Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); but how can I achieve loading all my common variable in My_Controller and accessing it to my other controllers? don't know how to do this. T_T best way to do this? - El Forum - 04-12-2011 [eluser]Wondering Coder[/eluser] anyone? T_T best way to do this? - El Forum - 04-12-2011 [eluser]InsiteFX[/eluser] Code: $this->variable; InsiteFX |