Information Technology Questions and Answers Community

Calling Controller in Code igniter Helper file

How can we call Controller in Code igniter Helper File? 

PHP         Framework        
Your Answer: 

1 Answers

The process follows two phases such as-

Phase1: Creating function in load_controller helper file with the name mentioned as load_controller. Also, you need to pass absolute path and create an object of that class file while loading the controller. It will automatically create an instance and behave as per the objectives.

<? 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();

    }

}

Phase 2: Now you can load the controller. At first you need to load the helper file and then you can load the helper function. As well as, you can pass controller and function that you are wanting in your controller or page.

$this->load->helper(‘load_controller’);

load_controller(‘homecontroller’, ‘view_name’);

Answered On 06 Apr at 09:40
Edit Account
LOGIN INFORMATION
Change Settings
Email Notifications Notifications will be sent to abcd@gmail.com
Select option for your email notifications:
Mature / News Filter We'll send on email when new jobs are posted
Option 2 for email notification We'll send on email when new jobs are posted