1

I created a core controller named Role_Admin and set the config prefix

$config['subclass_prefix'] = 'Role_';

Here is the code for the Role_Admin.php in the core folder

class Role_Admin extends CI_Controller {
    function __construct() {

    }
}

In the controller folder when I write

class admin extends Role_Admin { ... }

I get

Fatal error: Class 'Role_Admin' not found

Is something wrong in what I am doing.

edit: (i created a quick fix that is much better, any new core file that you create just extend the MY_Controller. Then in your controller directory you can extend any Core controller that you created

class MY_Controller extends CI_Controller {

        function __construct() {
            parent::__construct();

            //include custom core classes
            $core_path = DOCUMENT_ROOT . '/application/core';
            $this->load->helper('file');

            foreach(get_filenames($core_path) as $file) {
                if ($file != 'MY_Controller.php') {
                    if(file_exists($file)) {
                        include_once($file);
                    }
                }
            }
        }

}
3
  • where have you put this controller and which version are you using? Commented Nov 9, 2012 at 17:22
  • You have saved Role_Admin.php in application/core/ and not in system/core/, right ? Commented Nov 9, 2012 at 17:23
  • air4x, yes .... reheel i am using the latest as of now Commented Nov 9, 2012 at 19:08

1 Answer 1

1

The setting you have for $config['subclass_prefix'] is fine, but your file name is incorrect. CI is looking for the file Role_Controller.php, not Role_Admin.php.

There's an easier way to do this, and although it might appear to be a hack it is totally legit. Go back to the MY_ prefix, create MY_Controller.php, and in that file, just define the controller classes you want to use. You actually don't even need a MY_Controller class. Example:

// application/core/MY_Controller.php
class Role_Admin extends CI_Controller {}
class AnotherClass extends Role_Admin {}
class SomeOtherClass extends AnotherClass {}

All these classes will be available for your controllers to extend.

Sign up to request clarification or add additional context in comments.

3 Comments

have all these classes in that one file?
Yes, you can. It's what I do, but my base controllers are pretty lean.
Does my answer satisfy your question or is there still a problem? The issue with "custom core controller not found" was that you needed Role_Controller.php, not Role_Admin.php., if that was not the case then do let me know. The design of your classes is a separate concern.

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.