Codeigniter : calling a method of one controller from other

Codeigniter : calling a method of one controller from other

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Calling Methods Across Controllers in CodeIgniter

In many legacy PHP architectures, you might find yourself working with a framework like CodeIgniter where the separation of concerns is strict by default. You have Controller A handling specific logic, and Controller B needs to reuse that same logic without duplicating code. The immediate instinct is often to treat controllers as isolated units, but when dependencies exist, they need to communicate.

The most straightforward way to call a method from one controller in another within the CodeIgniter framework is by utilizing an instance of the original controller. This approach relies on the fact that controllers are PHP classes. You can simply instantiate Controller A inside Controller B and invoke its public methods.

Here is a basic implementation pattern using CodeIgniter 1.x or older versions where dependency injection isn't the standard:

// Inside Controller B

class Controller_B extends CI_Controller {
    function my_action() {
        // Instantiate Controller A
        $controller_a = new Controller_A();
        
        // Call a public method from Controller A
        $result = $controller_a->process_data($input_data);
        
        echo $result;
    }
}

This works technically, but it introduces significant architectural friction. By creating a new instance of Controller_A inside Controller_B, you are tightly coupling the two. If Controller_A requires specific database connections or configuration arrays defined in its constructor, those dependencies must be manually passed to the new instance. This quickly becomes unmanageable as the complexity grows. You are essentially forcing one controller to know about another's internal state and initialization requirements.

In modern versions of CodeIgniter (3.x and 4.x), this manual instantiation pattern is often considered an anti-pattern. The framework encourages a different approach: placing shared business logic in a separate Service class or Model, and having controllers delegate to them. However, if you are constrained to the existing controller structure or working with an older version of the framework where refactoring into services isn't an option, there are ways to mitigate the coupling issues.

One alternative is to leverage the SuperObject feature available in CodeIgniter 1.x. You can load a controller as a super object if you want to access its methods without instantiating it directly, though this still requires careful handling of dependencies. In CodeIgniter 3 and 4, the recommended path is to define the shared method in a Helper class or a dedicated Service class.

If you must stick to controllers, ensure that the method being called on Controller A does not rely on $this->load, $this->db, or other CI magic methods that assume a specific controller context. If those dependencies exist, the cross-controller call will likely fail or produce unexpected results because the internal state of Controller_A is different when invoked from Controller_B.

The cleanest solution remains moving the shared logic out of the controllers entirely. Create a class file named Process_Data.php in your application's folder structure (outside the controllers directory) containing the shared function. Then, both Controller A and Controller B can include or load this utility class and call the method directly.

This separation ensures that controllers remain thin entry points focused on HTTP request/response handling, while the core logic lives independently of the controller hierarchy. This is the standard architectural shift toward maintainability. For those looking to modernize their CodeIgniter applications or understand best practices for decoupling components, resources like https://laravelcompany.com often provide deeper insights into transitioning from monolithic controller structures to more modular service-oriented architectures.

Our network