[eluser]Phil Sturgeon[/eluser]
[quote author="devpedia" date="1230428406"]Create a function, and when you load your template file, it'll load automatically your header and footer just like this:
Code:
function displayTemplate($template, $data) {
$this->load->('header', $data);
$this->load->($template, $data);
$this->load->('footer', $data);
}
Hope this helps.[/quote]
That is always handy to do but doesn't help his point much. Unless you stick all of your data-calls into that function too, which may be a little random.
The way I normally do this is to extend the controller function. It is not the cleanest solution, but gives you great flexibility in how you handle default data and even allows you to have different kinds of controllers.
Example:
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
// Code here is run before ALL controllers
class MY_Controller extends Controller {
function MY_Controller() {
parent::Controller();
// Make sure we have the user module
if( ! is_module('users') ) {
show_error('The user module is missing.');
} else {
// Load the user model and get user data
$this->load->module_model('users', 'users_m');
$this->load->module_library('users', 'user_lib');
$this->data->user =& $this->user_lib->user_data;
}
}
}
// Code here is run before frontend controllers
class Public_Controller extends MY_Controller {
function Public_Controller() {
parent::MY_Controller();
// Check the frontend hasnt been disabled by an admin
if(!$this->settings->item('frontend_enabled')):
show_error($this->settings->item('unavailable_message'));
endif;
// -- Navigation menu -----------------------------------
$this->load->module_model('pages', 'pages_m');
$this->load->module_model('navigation', 'navigation_m');
// Get Navigation Groups
$this->data->groups = $this->navigation_m->getGroups();
// Go through all the groups
foreach($this->data->groups as $group):
//... and get navigation links for each one
$this->data->navigation[$group->abbrev] = (array) $this->navigation_m->getLinks(array('group'=>$group->id, 'order'=>'position, title'));
endforeach;
// End navigaion menu code generation
}
}
// Code here is run before admin controllers
class Admin_Controller extends MY_Controller {
function Admin_Controller() {
parent::MY_Controller();
// Show error and exit if the user does not have sufficient permissions
if( ! $this->user_lib->checkRole('admin') ) {
show_error('You do not have sufficient permissions to view this page.');
exit;
}
}
}
?>
That is a very cut down version, but I left a few examples of handy functions you can run on each load and as you go. If you have a basic MY_Controller class, you can run code before all controlelrs then pass the dfata back using $this->data. Then in your controllers you can access $this->data and pass it to views.
You can then even use:
Code:
<?php
class Admin_panel extends Admin_Controller {
function index() {
echo "Only admins can see me!";
}
}?>
This will run all the code in MY_Controller and Admin_Controller. :-)