10

I have strictly followed the how-to article by Phil Sturgeon, to extend the base controller. But I get still some errors.

My 3 classes:

// application/libraries/MY_Controller.php
class MY_Controller extends Controller{
    public function __construct(){
        parent::__construct();
    }
}

// application/libraries/Public_Controller.php
class Public_Controller extends MY_Controller{
    public function __construct(){
        parent::__construct();

    }    
}

// application/controllers/user.php
class User extends Public_Controller{
    public function __construct(){
        parent::__construct();
    }
}

Fatal error: Class 'Public_Controller' not found in /srv/www/xxx/application/controllers/user.php on line 2

Curious is that the following snippet is working, if I directly extends from MY_Controller:

// application/controllers/user.php
class User extends MY_Controller{
    public function __construct(){
        parent::__construct();
    }
}

I have loaded the controllers via __autoload() or manually. The controllers are loaded succesfully.

CI-Version: 1.7.3

3
  • Are you loading MY_Controller.php before Public_Controller.php is loaded? Commented Jan 5, 2011 at 22:03
  • Could you post the __autoload function in config.php? Commented Jan 7, 2011 at 10:31
  • There are much better answers in this question: stackoverflow.com/questions/21663045/… even as if it's marked as a duplicated of this Commented Jul 31, 2014 at 15:33

5 Answers 5

7

You need to require the Public Controller in your MY_Controller

// application/libraries/MY_Controller.php
class MY_Controller extends Controller{
    public function __construct(){
        parent::__construct();
    }
}

require(APPPATH.'libraries/Public_Controller.php');

You get the error because Public_Controller was never loaded. Doing this would allow you to extend from Public_Controller

I like what you are doing because I do that all the time.

You can do this also in your MY_Controller when you want to create an Admin_Controller

// application/libraries/MY_Controller.php
class MY_Controller extends Controller{
    public function __construct(){
        parent::__construct();
    }
}

require(APPPATH.'libraries/Public_Controller.php'); // contains some logic applicable only to `public` controllers
require(APPPATH.'libraries/Admin_Controller.php'); // contains some logic applicable only to `admin` controllers
Sign up to request clarification or add additional context in comments.

4 Comments

@Phil, he missed that part in your post :)
Oh. "I have loaded the controllers via __autoload() or manually." Who knows :)
To those want to use this snippet: Since version 2 Controller core class is changed to CI_Controller.
@Phil What about using hooks ? I have used this method, stackoverflow.com/a/22125436/567854
5

You should place Public_controller in with MY_Controller inside MY_Controller.php

// application/libraries/MY_Controller.php
class MY_Controller extends Controller{
    public function __construct(){
        parent::__construct();
    }
}

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

    }    
}

I use __construct everywhere and it works fine I recently wrote up an article on how to do this in relation to wrapping your auth logic into your extended controllers. It's about half way down when I start discussing constructing your controllers.

Comments

1

Problem was solved here: http://devcrap.net/pl/2011/09/04/codeigniter-dziedziczenie-z-my_controller-extends-my_controller/. In polish but code is good :]

Comments

1

I had problem like this,After some search I found error was made myself,Because my controller class name was MY_Controller but file name was My_Controller[Case not matching]. Note:- In localhost I didnt have any error.

In extended controller I Use

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

even I got the error.

After changing my file name to MY_Controller it started to work well.

Comments

0

I have a custom controller class called MY_Controller it extends CI_Controller and it works fine. It is located at application/core and it has custom functions lo load views in my site.

I use an abstract class My_app_controller that extends MY_Controller for my_app specific behabior, I want every controller in my_app to extend this abstract class. (I use diferent apps in the site, so some apps will extend My_app_controller and other apps will extend My_other_apps_controllers)

But when I try to extend My_app_controller from any controller in my application, "Main_app_controller extends My_app_controller" generates a Class 'My_app_controller' not found exception.

I found two solutions:

  1. use include_once in Main_app_controller.php file.
    include_once APPPATH.'controllers/path/to/My_app_controler.php';

  2. break the "one class per file" rule of code igniter and define my My_app_controller just in the same file MY_Controller is (under application/core).

Manual says:

Use separate files for each class, unless the classes are closely related

Well... they are.

Anyway, I prefered to use the include_once solution as I think it is better to have one file per class and My_app_controller is located under application/controllers/my_app folder. (so application/controllers/other_apps will exist)

Comments

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.