How to call Controller in Helper file In Codeigniter

In this tutorial we will learn how to call controller in helper file in codeigniter and render anywhere, whenever you want to do that.

In codeigniter php framework we can call controller in helper file and render in app/errors/error_404.php file. For calling you have to follow following steps.

Step one : create function in load_controller helper file with name load_controller when you will load controller that time you have to pass absolute path and create object of that class file it will create instance and behave whatever you want to use that controller features.

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

if (!function_exists('load_controller'))
{
    function load_controller($controller, $method = 'index')
    {
        require_once(FCPATH . APPPATH . 'controllers/' . $controller . '.php');

        $controller = new $controller();

        return $controller->$method();
    }
}

Step Two : now you are ready to load controller anywhere. First need to load helper file and then load function which inside helper and pass controller and function which you want to load in your page or controller.

$this->load->helper('load_controller');
load_controller('homecontroller', 'view_name');