Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter extending controller, controller not found

In Codeigniter 2.1.2 I want to create base controller and then extends from this controller. It does not work and I have no idea why and I'm pretty desperate now.

In \application\core\MY_Base_Controller.php I have this:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Base_Controller extends CI_Controller 
{
    function __construct()
    {
        parent::__construct();
...

In \application\controllers\Home.php I have this:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class Home extends MY_Base_Controller {

And the error message is

Fatal error: Class 'MY_Base_Controller' not found in ...\application\controllers\Home.php on line 3

I have no idea what to do, I read all over the internet that I have to put base controller to core folder what I did, that I have to name base controller with prefix MY_, I did. But it is still no working. And in my config.php is this line as well

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

Im running this on localhost using xampp

Thank you for your help

EDIT

Could someone please downlod following link try it, and tell me whats wrong. I have just downloaded codeigniter tried to create base controller and extend welcome controller. Not working. In following rar there are just modified files. Thank you http://goo.gl/sKHkDl

EDIT 2

I'm able to get this working by renaming MY_Base_Controller to MY_Controller. Does this mean, I'm able to create only one extended class for a controller ? eg. I can not have

  • MY_Base_Auth_Controller
  • MY_Base_Article_Controller

Just and only MY_Controller ?

like image 961
Harlsten Avatar asked Jan 25 '14 14:01

Harlsten


4 Answers

I had the same problem, but if I created all controllers in the MY_Controller.php file all worked well.

<?php

class MY_Controller extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        // do some stuff
    }
}

class MY_Auth_Controller extends MY_Controller
{
    function __construct()
    {
        parent::__construct();
        // check if logged_in
   }
}
like image 182
Jose Avatar answered Oct 22 '22 23:10

Jose


I've had the same issue in my first CI application and found two key elements that need to be checked:

1. Case Matching: Depending on your server configuration, the name of your file in the directory will need to match the case of your class. For instance, where your class is called "MY_Controller" your file name will need to be: "MY_Controller.php" on a Linux server. Windows servers have been known to have issues with uppercase filenames so you might experiment naming your controller "my_controller.php" and/or changing the extension to "my_" in your config.php instead of "MY_"

2. Insert an Autoloading function For reasons unknown to me, Codeigniter does not automatically recognize and read extended core classes before first loading the core class. This can cause issues with your extension not loading in correctly. To remedy this, you can add this simple autoloading script to the very bottom of your config.php

/*
|--------------------------------------------------------------------------
| Autoload Custom Controllers
|--------------------------------------------------------------------------
|
*/
function __autoload($class) {
    if (substr($class,0,3) !== 'CI_') {
        if (file_exists($file = APPPATH . 'core/' . $class . EXT)) {
            include $file;
        }
    }
}

Side note: the solution above was tested on CodeIgniter 2.1.4. The question asked involved CodeIgniter 2.1.2

like image 33
Websmith Avatar answered Oct 22 '22 23:10

Websmith


Anyone reading this using CI 3+ and trying to attempt the same thing. Please note that the global EXT was depreciated when dropping php 4 support. You need to use the following now:

/*
|--------------------------------------------------------------------------
| Autoload Custom Controllers
|--------------------------------------------------------------------------
|
*/
function __autoload($class) {
    if (substr($class,0,3) !== 'CI_') {
        if (file_exists($file = APPPATH . 'core/' . $class . '.php')) {
            include $file;
        }
    }
}
like image 10
Brian Ramsey Avatar answered Oct 23 '22 00:10

Brian Ramsey


Yes, with core's MY_ classes you can override ONLY codeigniter framework entities such as Controller, Model, Config, Exception and so on. Please refer to https://ellislab.com/codeigniter/user-guide/general/core_classes.html

like image 1
Alexander Yagovdik Avatar answered Oct 22 '22 23:10

Alexander Yagovdik