Welcome Guest, Not a member yet? Register   Sign In
Error extending core Controller
#1

[eluser]Chad Altemose[/eluser]
I'm trying to create a class in my application/libraries folder for extending the core Controller and adding some basic functionality that my controllers in application/controllers can use however i'm receiving the following error and i can't seem to figure out why having followed the CI user guide as well as having looked through the forums and other sites:

Code:
Fatal error: Class 'EX_Expenses' not found in {path_removed}/system/application/controllers/test.php on line 2

I have edited application/config/config.php so that the subclass prefix is 'EX_'

I have created the file application/libraries/EX_Expenses.php:

Code:
<?php
class EX_Expenses extends Controller {
    function EX_Expenses ()
    {
        parent::Controller();
    }    
}
?>

and am using it in the file application/controllers/test.php as such:

Code:
<?php
class Test extends EX_Expenses {
    function Test()
    {
        parent::EX_Expenses();
    }    
}
?>

I must be missing something simple but i've yet to find out how to fix this problem.

Suggestions?
#2

[eluser]TheFuzzy0ne[/eluser]
At what point are you including that file into your application?
#3

[eluser]Chad Altemose[/eluser]
What you see above is all that's there. The user guide under "Extending Core Class" seems to imply that by naming the subclass with the configured prefix as set in config.php and sticking in application/libraries would do the trick. Are you suggesting i need to load the 'EX_Expenses' class into my controller using something like:

Code:
$this->load->library('someclass');

or with the standard php include statement or something else?
#4

[eluser]TheFuzzy0ne[/eluser]
Yes, you can use a standard PHP include. In your first block of code, the Controller class is loaded automatically anyway, so no problems there. On your second block of code you will need to do something like this:

Code:
<?php

require_once(APPPATH.'libraries/ex_expenses'.EXT);

class Test extends EX_Expenses {
    function Test()
    {
        parent::EX_Expenses();
    }    
}

However, if you only want to extend the Controller, this code should work without any requires/includes:

./system/application/libraries/MY_Controller.php
Code:
<?php
# MY is the prefix in this case, and Controller is the class you want to extend.
# Change the prefix according to what you have in your config.php
class MY_Controller extends Controller {
    function MY_Controller ()
    {
        parent::Controller();
    }    
}

Then you can extend the MY_Controller class from your test controller.

./system/application/controllers/test.php
Code:
<?php
class Test extends MY_Controller {
    function Test()
    {
        parent::MY_Controller();
    }    
}

Also note that there's not need for the closing PHP tag, and sometimes it actually causes problems.

Hope this helps.
#5

[eluser]Chad Altemose[/eluser]
Thanks for the help. If I leave things as i have them but make one addition to the test controller (./system/application/controllers/test.php) as you noted:

Code:
require_once(APPPATH.'libraries/ex_expenses'.EXT);

then things work as expected. i had to add an index function to actually have something display on screen (obviously) but i received no errors.

However, I do indeed only want to extend the controller in my EX_Expenses class (or MY_Controller as you've shown) but when I set things up as you have shown which is essentially identical to what i've pasted into my original post, i get that 'class not found' error i've noted above. I did change my original code to leave off the closing php tags but that has no effect on the error.

As far as I can tell, everything is setup as it should be:
-changed $config['subclass_prefix'] in the application/config/config.php file to be my custom prefix 'EX_'
-put 'EX_Expenses.php' into application/libraries/ with code as shown above (essentially identical to what you've shown)
-referenced it appropriately in application/controllers/test.php

I'm fine with using the 'require_once' solution but i sure would like to know why the solution without the include doesn't work when I seem to be following the requirements for extending the core controller this way.
#6

[eluser]TheFuzzy0ne[/eluser]
Did you rename the file accordingly? If so, please repost your code so I can be certain of what you have - including filenames in the exact case you have them.
#7

[eluser]Chad Altemose[/eluser]
Ok - so first off - in ./system/application/config/config.php i reset the subclass_prefix to its default of "MY_" to keep things simple.

My base class that extends the core Controller for other classes to use is here:
./system/application/libraries/MY_Expenses.php

and is exactly this:

Code:
<?php
class MY_Expenses extends Controller {
    function MY_Expenses()
    {
        parent::Controller();
    }
    function index()
    {
        echo "MY_Expenses index";
    }
}

My test class which uses the MY_Expenses class is here:
./system/application/controllers/test.php

and is exactly this:

Code:
<?php
//require_once(APPPATH.'libraries/MY_Expenses'.EXT);

class Test extends MY_Expenses {
    function Test()
    {
        parent::MY_Expenses();
    }
}

As is shown above i get the error:
Code:
Fatal error: Class 'MY_Expenses' not found in {path_removed}/system/application/controllers/test.php on line 5

If I uncomment the 'require_once' line in the Test class then I get the expected results with "MY_Expenses index" echoing to the browser.

That's it.
#8

[eluser]TheFuzzy0ne[/eluser]
The bit after the prefix must be the name of the CodeIgniter class you want to extend, so your code should be.
./system/application/libraries/MY_Controller.php
Code:
<?php # CodeIgniter will now load this file automatically when the Controller class is loaded.

class MY_Controller extends Controller { # Extend the Controller class
    function MY_Controller()
    {
        parent::Controller();
    }
    function index()
    {
        echo "MY_Expenses index";
    }
}

./system/application/controllers/test.php
Code:
<?php
//require_once(APPPATH.'libraries/MY_Expenses'.EXT);

class Test extends MY_Controller { # extend MY_Controller
    function Test()
    {
        parent::MY_Controller();
    }
}
#9

[eluser]Chad Altemose[/eluser]
Ah - I knew it was something simple. Thanks a ton.
#10

[eluser]Unknown[/eluser]
Sorry, I guess I can't delete my post by myself (ignore it, please).
I was getting a similar error but it was my fault! :down:




Theme © iAndrew 2016 - Forum software by © MyBB