Welcome Guest, Not a member yet? Register   Sign In
Using a function across multiple controllers
#1

[eluser]Mowgli[/eluser]
Hello,

Is there any way i could use a (externalized) function across multiple controllers ?
A possible answer would be using a helper but i cannot load lang files as it is not an object.

Here's a function from the default controller:

Code:
function account()
    {
        //START TEMPLATE
        $data['header'] = 'includes/header';
        $data['main_content'] = 'account';
        $data['footer'] = 'includes/footer';
        $data['menu'] = 'menu';
        //END TEMPLATE
        
        //START LANGUAGE FILES
        $this->lang->load('account','english');
        $this->lang->load('header','english');
        $this->lang->load('footer','english');
        $this->lang->load('menu','english');
        //END LANGUAGE FILES
        
        $this->load->view('includes/template', $data);
    }

I wouldn't normally do this but here's my problem. I decided do make a controller that handles all the form inputs across my website.

When i use form validation i actually have to reload the views (to display the errors) so i need to call that function. If i use redirect it won't work (obviously). One solution (that works) would be to copy the function(s) into my new controller but that won't help me as i don't want to have the same piece of code everywhere.

I even tried extending my default controller in order to inherit the above function but for some reason it won't work.

Any ideas ? Thanks in advance.
#2

[eluser]Mowgli[/eluser]
Nevermind, i figured out by myself.
I created my own library and used $CI =& get_instance(); instead of $this.
#3

[eluser]Aken[/eluser]
You can use get_instance() inside your helper functions also, nothing wrong with that.
#4

[eluser]pickupman[/eluser]
Extending the controller would have worked, if you made $data a property of the parent controller. Then either in that parent controller, or a child controller you can reference it via $this->data. What you are doing is somewhat common to create a display method for in a controller. I use a similar idea by creating a display method that will look in a views subfolder named after the controller and load a file matching the method. If no match if found, it will look in the views folder. If that fails, I check if a view has been explicitly sent that may not match the pattern. Then check if content is being sent to the browser (sometimes ajax stuff). This is all then sent to the template library. Also loading language files in a similar fashion when necessary.

Code:
class MY_Controller extends CI_Controller{

  public $data;

  public function __construct(){
      parent::__construct();

      $this->data['something_important'] = 'me'; //Make something available to all child classes
  }

function account()
    {
        //START TEMPLATE
        $this->data['header'] = 'includes/header';
        $this->data['main_content'] = 'account';
        $this->data['footer'] = 'includes/footer';
        $this->data['menu'] = 'menu';
        //END TEMPLATE
        
        //START LANGUAGE FILES
        $this->lang->load('account','english');
        $this->lang->load('header','english');
        $this->lang->load('footer','english');
        $this->lang->load('menu','english');
        //END LANGUAGE FILES
        
        $this->load->view('includes/template', $this->data);
    }  

}

//Now in child controller
class Child extends MY_Controller{
    
   public function __construct(){
       parent::__construct();
   }

   public function index(){

      //$this->data can be seen in parent & child
      $this->data['more_content'] = 'Something added here';

      //Change ['something_important']
      $this->data['something_important'] = 'Good coding';

      //Run the account method in the parent
      $this->account();
   }
}




Theme © iAndrew 2016 - Forum software by © MyBB