Welcome Guest, Not a member yet? Register   Sign In
Auto including data in my view
#1

[eluser]ixxalnxxi[/eluser]
Hello All,

I anticipate having 15 or so controllers, all which load the 'header' view. In the 'header' view I will be sending data from a function in a library, so each header will be loaded as

$this->load->('header', $data);

Rather then explicitly sending the parameter in for every load call of header, is there a way to automatically append data to a view?

Edit: There were some details I forgot, the $data passed to header will always be reading from the same function, so any place where I can write this piece of code once and auto-it into header would be nice.
#2

[eluser]darkhouse[/eluser]
I think the solution is to grab the data from the library function right in the header view. You'll always need to load the view, but this way you don't need to have $data['something'] = $this->some_library->some_func(); in every controller. Even though I guess it technically breaks the MVC format, just do that right from the view and that's that.

It might make sense to autoload the library also, if you're not already. That way you won't need to load it in each controller either.
#3

[eluser]ixxalnxxi[/eluser]
I am autoloading the library at the moment. So you're saying that right in the view I can use methods specific to the library? So in the header I can do (auto loaded library called Login):

$this->login->display_header(); ?
#4

[eluser]darkhouse[/eluser]
Yep. I did it that recently with something. I had a nav menu view and one of the items was Alerts. The link needed to change color to red and bold if the user had new alerts, and put the number of alerts in, like "Alerts (2)". Since I didn't want to do this on every controller, it made more sense to just write a library and put in the view something like $this->alerts->display_link();
#5

[eluser]bitist[/eluser]
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);
}

Then create 2 library files: header.php , footer.php and 2 view files in the views directory. The view files will be loaded by library files.

Hope this helps.
#6

[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. :-)




Theme © iAndrew 2016 - Forum software by © MyBB