Irfana
Irfana

Reputation: 231

Accessing controller's function

I am using Codeigniter framework to develop a website. I am currently working on home.php view under the view folder. I need to use UserInfo() function which is inside one of the controllers. Any suggestion how to access that function?

class Welcome extends CI_Controller {

            public function UserInfo(){

            $this->load->model('model_user');                
            $data['title'] = 'Users';              
            $data['users'] = $this->model_user->getUser();              
            $this->load->view('template/users', $data);
        }
}

Upvotes: 0

Views: 77

Answers (2)

Maninderpreet Singh
Maninderpreet Singh

Reputation: 2587

As You want to call controller function in other controller.In codeigniter App folder core folder exists you make a custom core controller and all other controllers extend with your custom controller

In your Custom Core controller

class CustomCore extends CI_Controller
{
     /* ---YOUR FUNCTION IN CUSTOMCORE---- */

      public function mycorefunc()
      {
           //Do something
      }

}

and your all other controllers extend with custom core

class YourController extends Customcore
{

      function controllerfunction()
      {
            $this->mycorefunc();// Call corefunction 
      }

}

Upvotes: 0

Abdulla Nilam
Abdulla Nilam

Reputation: 38584

You cant call controller method inside another controller. Its No Way to do it.

You have two way to resolve this issue

  1. If you want to access the function which place inside the controller, add that into an model. So by loading model you can call it.
  2. use redirect('welcome/UserInfo') if you just need to call the function

Upvotes: 1

Related Questions