CodeIgniter Forums
--Is there a controller hierarchy ? - 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: --Is there a controller hierarchy ? (/showthread.php?tid=3653)



--Is there a controller hierarchy ? - El Forum - 10-15-2007

[eluser]Unknown[/eluser]
Hi I am a newbie to CI. I created a controller which extends the Controller:

class BaseSessionController extends Controller {
// some logic here
}

Then I tried to create another controller class which extends the BaseSessionController:

class SubController extends BaseSessionController {
// some logic here
}

These two controllers are in two different php files -- both in the controllers folder. It seems that BaseSessionController could not be recognized by SubController . I am wondering if I have to manually include the BaseSessionController script file in my SubController script, using require() or require_once().


--Is there a controller hierarchy ? - El Forum - 10-16-2007

[eluser]CodeOfficer[/eluser]
yep, you have to manually include it unfortunately


--Is there a controller hierarchy ? - El Forum - 10-16-2007

[eluser]Matthew Pennell[/eluser]
If it's not being used as a 'real' controller (i.e. you're only sub-classing it), you could stick it in the /libraries folder - that way you can autoload it and won't have to require/include it.


--Is there a controller hierarchy ? - El Forum - 10-16-2007

[eluser]Unknown[/eluser]
I tried the library approach, but it can only be accessed through composition but not inheritance.

Specifically, I first created a script:

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

class Someclass {

function some_function()
{
}
}

?>

Then put it into the library folder, and modified the autoload.php to auto-load it.

After that I can access it in other controllers through composition like:
$this->someclass->some_function();

But still I cannot sub-class it like:

class SubClass extends Someclass {
// some logic here
}


--Is there a controller hierarchy ? - El Forum - 10-16-2007

[eluser]CodeOfficer[/eluser]
Yeah, I had wanted to do the same at one point and was only able to pull it off when I hardcoded the include.