CodeIgniter Forums
Extending the Controller, Twice - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Extending the Controller, Twice (/showthread.php?tid=23690)

Pages: 1 2


Extending the Controller, Twice - El Forum - 10-19-2009

[eluser]AlexJ[/eluser]
Ive been developing in CI for a while, but I cant really get my head around this one:

Extending the Controller works just fine:

Code:
class V_Controller extends Controller
{
    /**
     * V_Controller::V_Controller()
     *
     * @return void
     */
    public function V_Controller()
    {
        parent::Controller();
    }

    /**
     *  The rest of my code goes here
     */
}

It also works in my default controllers, but when I try to do this:

Code:
class V_Module extends V_Controller
{
    /**
     * V_Module::V_Module()
     *
     * @return void
     */
    public function V_Module()
    {
        parent::V_Controller();
    }
}

And try to run a controller with the V_Module as parent, I get the following error:

Fatal error: Class 'V_Module' not found in xx\application\controllers\webshop\producten.php on line 12

Ive searched the board and came across the require once solution, I dont want to require the controller in each module.

Shouldnt the class V_Module be loaded autoloaded because it is in my libraries folder?

Am I missing something?


Extending the Controller, Twice - El Forum - 10-19-2009

[eluser]BrianDHall[/eluser]
I've never tried this myself, but have you tried sticking V_Module at the end of your autoload module list in autoload.php?

Nothing is autoloaded just because it is in the libraries folder, you'll have to specify you want it loaded somewhere.


Extending the Controller, Twice - El Forum - 10-19-2009

[eluser]pistolPete[/eluser]
A library is only autoloaded if you use the MY_ prefix (and extend a core class), so put the class in a file called MY_Controller.php.
--> http://ellislab.com/codeigniter/user-guide/general/core_classes.html


Extending the Controller, Twice - El Forum - 10-19-2009

[eluser]AlexJ[/eluser]
Thanks for your reply's guys, ive set my prefix to V_ instead of MY_.

I basicly want this structure:

Code:
-CI Controller
   - V_Controller extends CI Controller
   - Controller 1 extends V_Controller
   - Controller 2 extends V_Controller
     V_Module extends V_Controller
     - Controller 3 extends V_Module
     - Controller 4 extends V_Module

Ive added an autoload for V_Module (also tried naming it Module, didnt work) but it doesnt seem to include the V_Module class, a var dump of get_included_files before the class not found exception:

Code:
array(25) {
  [0]=>
  string(29) "index.php"
  [1]=>
  string(47) "\system\codeigniter\CodeIgniter.php"
  [2]=>
  string(42) "\system\codeigniter\Common.php"
  [3]=>
  string(42) "\system\codeigniter\Compat.php"
  [4]=>
  string(52) "\application\config\constants.php"
  [5]=>
  string(49) "\application\config\config.php"
  [6]=>
  string(43) "\system\libraries\Benchmark.php"
  [7]=>
  string(39) "\system\libraries\Hooks.php"
  [8]=>
  string(40) "\system\libraries\Config.php"
  [9]=>
  string(48) "\application\config\hooks.php"
  [10]=>
  string(57) "\application\hooks\load_interfaces.php"
  [11]=>
  string(54) "\application\interfaces\V_Index.php"
  [12]=>
  string(55) "\application\interfaces\V_Create.php"
  [13]=>
  string(53) "\application\interfaces\V_Edit.php"
  [14]=>
  string(55) "\application\interfaces\V_Delete.php"
  [15]=>
  string(37) "\system\libraries\URI.php"
  [16]=>
  string(40) "\system\libraries\Router.php"
  [17]=>
  string(49) "\application\config\routes.php"
  [18]=>
  string(40) "\libraries\Output.php"
  [19]=>
  string(39) "\system\libraries\Input.php"
  [20]=>
  string(42) "\system\libraries\Language.php"
  [21]=>
  string(41) "\system\codeigniter\Base5.php"
  [22]=>
  string(44) "\system\libraries\Controller.php"
  [23]=>
  string(58) "\application\libraries\V_Controller.php"
  [24]=>
  string(65) "\application\controllers\webshop\producten.php"
}

Note that I have a hook that loads some interfaces that I need, when is the autoload of libraries actually called?


Extending the Controller, Twice - El Forum - 10-19-2009

[eluser]pistolPete[/eluser]
A file with the subclass prefix will only be included automatically if it extends a core library!
Since there is no Module core class, your V_Module won't be included.


Extending the Controller, Twice - El Forum - 10-19-2009

[eluser]AlexJ[/eluser]
Ive read that pistolPete Wink

Fatal error: Class 'Module' not found in application\controllers\webshop\producten.php on line 13

I renamed it to all kinds of stuff, including without prefix, autoloading the module doenst include it


Extending the Controller, Twice - El Forum - 10-19-2009

[eluser]pistolPete[/eluser]
Sorry.
Quote:when is the autoload of libraries actually called?

The libraries are autoloaded from within the Controller class' constructor, which (in your case) is too late.


Extending the Controller, Twice - El Forum - 10-19-2009

[eluser]AlexJ[/eluser]
What would seem the cleanest:

1. Fix it with a including hook on pre_system
2. Require in the index.php file

I want as little code in my Module files as possible.

Thanks for clearing a lot of things up pete


Extending the Controller, Twice - El Forum - 10-19-2009

[eluser]pistolPete[/eluser]
There are more options:

4) Put both classes (V_Controller and V_Module, in that order) in V_Controller.php
5) require V_Module.php from within V_Controller.php


Extending the Controller, Twice - El Forum - 10-19-2009

[eluser]AlexJ[/eluser]
4. Is a little bit of a code smell :p
5. Is brilliant and works like a charm

Thanks a lot! Wink