0

I need a hand here guys, I am confuse on how I will be placing my files for my code to work out. I had 2 controllers named

ex: 
controller1.php
controller2.php

which I want all my functions in controller1.php accessible in controller2.php so I write

class Controller2 extends Controller1 { 

}

which I had applied what I had understand here Calling a Controller function in another Controller in CodeIgniter

But I am getting an error

Fatal error: Class 'Controller1' not found in C:\xampp\htdocs\bla\application\controllers\Controller2.php on line 3

Then I had search for any possible solution then i had found out that i had to place Controller1.php in the core folder but I am getting the same error..MY_Controller class not found

I am using the latest version of codeigniter framework.

What I had missed? How?

1
  • I think every controller is auto loaded. why don't you take the instance of another controller class to your controller. Commented Oct 17, 2013 at 7:37

2 Answers 2

0

Rename the file and class to "MY_Controller" if you placed it into the "/core" folder. The prefix is important (which you can set in your config, by the way). Also, make sure, that in MY_Controller, you extend the CI_Controller

class MY_Controller extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }
}

You can then access this core-Controller with your other Controllers like this:

class Controller1 extends MY_Controller
{
    public function __construct()
    {
        parent::_construct();
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

This explains what I had in mind.. But how do i tweak the name of the file and name of the class e.g. MY_Controller because for some reason, I want my file to be relevant to what was on the code
Well, the core controller has to be named that way (<prefix>_Controller) for CodeIgniter to find it. As far as I know, the only thing you can change is the prefix
You don't have to extend the base controller if not needed. Just put your class in MY_Controller.php and it will be loaded.
Aah, that's true, it's not a necessity to extend the CI_Controller. Thanks for that clarification :) In my cases, I always extended the base controller
0

Controller1 will be auto-loaded and be able to use for extending if you place it inside application/core/MY_Controller.php.

Otherwise, you have to use require:

require APPPATH . 'controllers/Controller1.php';
class Controller2 extends Controller1 { }

2 Comments

I think this also a good one and more readable but the drawback is that you have to include the file in all your class.. but thank you for the input @anvoz coz if the above code will not work then I would definitely use this one..
If you need to use require in a lot of files then Controller1 should be a base controller defined in MY_Controller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.