Welcome Guest, Not a member yet? Register   Sign In
error when extending CI_Controller
#1

[eluser]Kenneth Vogt[/eluser]
I am using CodeIgniter 2.1.0. My application/config/config.php file includes:

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

I would like to extend CI_Controller, so I created the file /application/core/MY_Controller.php with the following code:

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

class MY_Controller extends CI_Controller {

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

/* End of file MY_Controller.php */
/* Location: ./application/core/MY_Controller.php */

(I realize it doesn't do anything yet, and that will kind of be the point.)

I next changed my 'users' controller (which is working just fine) from:

Code:
class Users extends CI_Controller {

to:

Code:
class Users extends MY_Controller {

Now, when I call that controller, I get a 500 error. What am I missing?
#2

[eluser]InsiteFX[/eluser]
It needs to be saved in ./application/core/MY_Controller.php

500 is a server error nothing to do with CodeIgniter.
#3

[eluser]Kenneth Vogt[/eluser]
As noted above, it is saved in ./application/core/MY_Controller.php. Am I misunderstanding you?

Also, while it is correct to say that 500 errors are server errors, it is not correct to say it has nothing to do with CodeIgniter. A coding mistake can easily result in a 500 error. For instance, attempt to autoload a library where the library class is missing its closing bracket: you get a 500 error.
#4

[eluser]Kenneth Vogt[/eluser]
After upping my error reporting (thanks to the post by kb1ibh at http://ellislab.com/forums/viewthread/218167/), I got this additional information:
Code:
Fatal error: Class 'MY_Controller' not found in {website}/application/controllers/users.php on line 3
As you can see from the code above, line 3 is where I extend MY_Controller. So it would appear that CodeIngniter is not finding ./application/core/MY_Controller.php. The question is why not?
#5

[eluser]InsiteFX[/eluser]
Try adding this: ./application/config/config.php!
Code:
/*
| -------------------------------------------------------------------
|  Native Autoload - by Phil Sturgeon. New Version!
| -------------------------------------------------------------------
|
| Nothing to do with config/autoload.php, this allows PHP autoload to work
| for base controllers and some third-party libraries.
|
| If using HMVC you do not need this! HMVC will autoload.
|
| Place this code at the bottom of your application/config/config.php file.
*/
function __autoload($class)
{
    if (strpos($class, 'CI_') !== 0)
    {
        if (file_exists($file = APPPATH . 'core/' . $class . EXT))
        {
            include $file;
        }
        elseif (file_exists($file = APPPATH . 'libraries/' . $class . EXT))
        {
            include $file;
        }
    }
}

Please read the code comments...
#6

[eluser]Kenneth Vogt[/eluser]
I added the code above to the end of ./application/config/config.php and I still get the same error. I even tried putting a copy of MY_Controller.php at ./application/libraries. I'm stumped.
#7

[eluser]Aken[/eluser]
If you add this to the top of your MY_Controller.php file and reload, does the message show up?

Code:
exit('MY_Controller.php loaded');

If so, then the file is being found, but it is not being extended properly. Perhaps you misspelt it (you didn't in your quoted code above, but maybe you changed it by mistake).

If not, then the file is not being loaded, and might be a problem with the path or permissions. Make sure the folder has at least read permissions (typical CHMOD is 755 for directories that don't need write permissions). Make sure your application folder path is correct in index.php.

The file is included in system/core/CodeIgniter.php somewhere around line 235 (when it works properly, lol).

If you have any other code or addons that might interfere also, check those.
#8

[eluser]Kenneth Vogt[/eluser]
It was a permissions problem. I am using PhpFog and they automatically install CodeIgniter, so I will take it up with them why anything from ./application on down is not readable. Thanks for all your help, InsiteFX and Aken.

One other thing worthy of note, http://www.php.net/manual/en/language.oop5.autoload.php says:

"spl_autoload_register() provides a more flexible alternative for autoloading classes. For this reason, using __autoload() is discouraged and may be deprecated or removed in the future."

This applies from PHP 5.3.0, so check your PHP version.

Here is an updated version of Phil Sturgeon's code:
Code:
spl_autoload_register(function ($class) {
  if (strpos($class, 'CI_') !== 0)
  {
    if (file_exists($file = APPPATH . 'core/' . $class . EXT))
    {
      include $file;
    }
    elseif (file_exists($file = APPPATH . 'libraries/' . $class . EXT))
    {
      include $file;
    }
  }
});
#9

[eluser]InsiteFX[/eluser]
Thank's Ken,

I added it to my snippets with credit to you.
#10

[eluser]nagata[/eluser]
Hi, I have been ssearching forum now, and found this code (Last Ken's Reply)
I been wondering how to use this code? Cause when I place it in place of the old Phil's Autoloader it dosent work for me...
I get this error
Code:
Parse error: syntax error, unexpected T_FUNCTION, expecting ')' in C:\xampp\htdocs\application\config\config.php on line 395




Theme © iAndrew 2016 - Forum software by © MyBB