1

I have created custom controller called "MY_Controller.php" in Application/core, and successfully invoked by inheriting through application controller.

//application/core
class MY_AdminController extends CI_Controller {
function __construct() {
        parent::__construct();            
    }
 }

//application/controllers
class User extends MY_AdminController {
    public function __construct(){
          parent::__construct();
    }
}

It works fine. I just changed my file name from "MY_Controller.php" to MY_AdminController.php, and following same class name, but it is throwing following error,

Fatal error: Class 'MY_AdminController' not found

As per the documentation, Whenever you create a class with the MY_ prefix the CodeIgniter Loader class will load this after loading the core library, then why its throwing error...!!!

Ijas Ameenudeen
  • 9,069
  • 3
  • 41
  • 54
EKL
  • 143
  • 3
  • 13

5 Answers5

2

Go to your config.php and change

$config['subclass_prefix'] = 'MY_'; to $config['subclass_prefix'] = 'MY_Admin';
eebbesen
  • 5,070
  • 8
  • 48
  • 70
Laukik Patel
  • 733
  • 7
  • 18
  • 1
    Thanks...Its working fine, How can I create multiple custom classes with different name? is that not possible? – EKL Mar 07 '14 at 10:49
1

Expanding on Patel, the issue is that MY_ is the prefix to the original core files.

Controller, Model, View etc.

MY_ will be used to seek the name of the controller, for example, MY_controller searches for CI_controller.

You cannot load random names using the MY_prefix. you use MY_ to extend the already existing names.

Patrick
  • 3,289
  • 2
  • 18
  • 31
0

I think the problem is with the class name, you may have not changed the class name from MY_Controller to MY_AdminController

Nouphal.M
  • 6,304
  • 1
  • 17
  • 28
0

You can use custom classes. But CI only loads the classes with the class prefix in the config file (e.g. MY_). So I explained how it works and created a workaround to load classes automatically. You can find it here https://stackoverflow.com/a/22125436/567854.

Hope this helps :)

Community
  • 1
  • 1
Ijas Ameenudeen
  • 9,069
  • 3
  • 41
  • 54
0

If you want to include all the files that are in your core folder. Then write the following code in end of config.php file.Path to file is application/config/config.php

function __autoload($class)
{
    if (strpos($class, 'CI_') !== 0)
    {
         @include_once( APPPATH . 'core/' . $class . EXT );
    }
}

By using this you can create multiple classes.

SMNTB
  • 789
  • 7
  • 8
  • Better use hooks rather than including a function in file (**`config.php`**) which is meant to contain only configurations, not any functions. If you still want to use above method, better include this function in **`application/config/autoload.php`**, it will be more meaningful. – Ijas Ameenudeen Mar 17 '14 at 04:02