问题
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 ?
回答1:
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
}
}
回答2:
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
回答3:
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;
}
}
}
回答4:
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
回答5:
I faced same issue. I think the problem is, CI only loading only single file which must be named as "MY_controller.php". (system/core/Codeigniter.php - line 238). It is made to load only "MY_Controller.php". Answers made above like "_autoload" are there because we are overriding that functionality. I guess its in both i.e CI -2 and CI - 3. Either we can make changes at core files or we can use autload function, as mentioned above.
thanks,
来源:https://stackoverflow.com/questions/21351808/codeigniter-extending-controller-controller-not-found