How to call this function for every controller in codeigniter

1.9k views Asked by At

My situation is that, I have 20 controllers in my website, where I have a function like this.

$this->lang->load("main", $this->session->userdata("lang_code"));

How can I load this function in all the 20 controllers without adding it individually to the __constructor of every controller?

2

There are 2 answers

2
Harigovind R On BEST ANSWER

You can create a library for this purpose and autoload this library. Creating a library is explained in the given link codeigniter library

Or you can refer to a somewhat same question asked in the stackoverflow Stack overflow post

1
parth On

try this

create the my_controller.php in application/core

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class My_controller extends CI_Controller {

       function My_controller()
       {
            parent::__construct();
            $this->lang->load("main", $this->session->userdata("lang_code"));

       }
    }
 ?>

in your controller

 class Your_controller extends MY_Controller {

 }