Please wait for 15 seconds...

Friday, 27 July 2012



There are many libraries that you commonly initialize in every controller in codeigniter, so you load them one by one in every controller. But if you load these libraries in My_Controller you can call library methods in every controller. So lets get start it.

First create My_Controller class in application/core.

Remember filename must be MY_Controller.php else codeigniter will throw some error.

Now extend Codeigniter's main controller(CI_Controller), so we can use its properties and methods in My_Controller class


<?php
class MY_Controller extends CI_Controller{
    public function __construct(){
        parent::__construct();
        $this->load->library('session'); // loading session class
        $this->load->library('my_custom_library'); // loading library class
    }
}

Now we are ready to extent MY_Controller in other controllers.

<?php
class Home extends MY_Controller{
    public function index(){
           $this->my_custom_library->my_method();
    }
}

*You can also change MY_ prefix through config.

Happy coding..

Posted by Atul Prasad Gupta

0 comments:

Post a Comment

Subscribe Techsirius

Techsirius on Facebook