best way to do this? |
[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?
[eluser]shadow player[/eluser]
Add them to autoload: http://ellislab.com/codeigniter/user-gui...oader.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 =)
[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.');
[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.');
[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.
[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.
[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
|
Welcome Guest, Not a member yet? Register Sign In |