user1275378
user1275378

Reputation:

CodeIgniter call same function for all controllers

I'm developing an application, where the same function I'm writing for some controllers. So I wanted one common function for all controllers. Please help me out I've stucked up with this. The work would be more appreciated.

Upvotes: 3

Views: 2921

Answers (3)

Bilal
Bilal

Reputation: 2673

You can develop a helper or library.

codeigniter version 2 : https://codeigniter.com/userguide2/general/creating_libraries.html

codeigniter version 3 : https://codeigniter.com/user_guide/general/creating_libraries.html?highlight=library

Upvotes: 3

Rimjhim Ratnani
Rimjhim Ratnani

Reputation: 113

create a model class and create that all function after that extend that model all model classes .you can use one function different controller.other wise you can use Cms helper.this is the best way call a common function in different controller.

Upvotes: 0

Muhammad Raheel
Muhammad Raheel

Reputation: 19882

You can create MY_Controller class in application/core

Class MY_Controller Extends CI_Controller{

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

    public function common(){
        // code here
    }
}

Now extend every class with MY_Controller and the function will be available for each class

Class Test Extends MY_Controller{

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

When in the url you call

http://localhost/app/test/common

This will work.

Upvotes: 2

Related Questions