Welcome Guest, Not a member yet? Register   Sign In
best way to do this?
#1

[eluser]Wondering Coder[/eluser]
hi everyone.,

I'm building an application using CI and using this structure most of my controllers:
Code:
function index(){
                $data['mod'] = strtolower(get_class($this));
        $data['role'] = $this->session->userdata('data_id');        
        $dt = $this->session->userdata('user_id');
        ....
                .........
        
        $data['content'] = $this->load->view('panes/com_profile', $data, true);

        $this->load->vars($data);
        $this->load->view('template');
}

function insert(){
                $data['mod'] = strtolower(get_class($this));
        $data['role'] = $this->session->userdata('data_id');        
        $dt = $this->session->userdata('user_id');
        ....
                .........
        
        $data['content'] = $this->load->view('panes/insert', $data, true);

        $this->load->vars($data);
        $this->load->view('template');
}

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?
#2

[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 =)
#3

[eluser]toopay[/eluser]
use 'pre_controller' and 'post_controller' hooks.

enable hooks in 'application/config'
Code:
$config['enable_hooks'] = TRUE;
then write hook configuration
Code:
$hook = array(
        'pre_controller'   => array(
                                'class'    => 'Bootstrap',
                                'function' => 'before',
                                'filename' => 'Bootstrap.php',
                                'filepath' => 'hooks',
                                'params'   => NULL
                                ),
        'post_controller'  => array(
                                'class'    => 'Bootstrap',
                                'function' => 'after',
                                'filename' => 'Bootstrap.php',
                                'filepath' => 'hooks',
                                'params'   => NULL
                                ),
        );
Now you can do your stuff. Write a new file, called 'Bootstrap.php' under 'application/hooks', then write something like...
Code:
<? defined('BASEPATH') or die('No direct script access allowed.');

class Bootstrap {

protected $_CI;

  public function __construct(){
     $this->_CI =& get_instance();
  }

  public function before(){
     // Do your pre-controller stuff
     // .....
  }

  public function after(){
     // Do your post-controller stuff
     // .....
     $this->_CI->load->vars($data);
     $this->_CI->load->view('template');
  }

}
#4

[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.');
#5

[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.
#6

[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.
#7

[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');

class MY_Controller extends CI_Controller {
    /**
     * Class Constructor
     *
     * @access    public
     * @return    void
     */
    public function __construct()
    {
        parent::__construct();
    }
    
       public function is_logged_in()
    {
        $is_logged_in = $this->session->userdata('is_logged_in');
        if(!isset($is_logged_in) || $is_logged_in != true)
        {
            echo 'You don\'t have permission to access this page. <a href="../">Login</a>';    
            die();        
        }        
    }
}

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
#8

[eluser]Wondering Coder[/eluser]
anyone? T_T
#9

[eluser]InsiteFX[/eluser]
Code:
$this->variable;

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB