CodeIgniter Forums

Full Version: Extending Controller?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]iConTM[/eluser]
I just can't figure out how to extend the controller class properly in CI

Let's say I want to have the following tree:

Code:
// ./system/libraries/
Controller

// ./application/libraries/
MY_Controller extends Controller

// ./application/controllers/
ControllerA extends MY_Controller
ControllerAa extends ControllerA
ControllerAb extends ControllerA

ControllerB extends MY_Controller
ControllerBa extends ControllerB

It works fine until level 3 (ControllerAa, ... )

Is this possible in CI?

regards

El Forum

[eluser]intractve[/eluser]
Is there any particular reason you would like to do this?

El Forum

[eluser]iConTM[/eluser]
Yes there is Wink

Code:
// ./system/libraries/
Controller

// ./application/libraries/
MY_Controller extends Controller

// ./application/controllers/
ControllerA extends MY_Controller
Login extends ControllerA
Logout extends ControllerA

ControllerB extends MY_Controller
Welcome extends ControllerB
...

I would like to seperate my login and logout controllers from my other controllers (welcome, ...). but all those inherit from one main controller (MY_Controller).

not the Controller in the system folder cause I would like to add some code to it Confused

El Forum

[eluser]intractve[/eluser]
To me it seems like a lot of work to do something simple.
I have written many web applications with codeigniter and never once had to extend the controller to bend it to my needs, I have extended a lot of the inbuilt libraries but never the Controller.

But to each his own, I guess.

Have fun finding out stuff in here... Maybe someone can help you with it.
Cheers

El Forum

[eluser]davidbehler[/eluser]
Code:
require_once(BASEPATH.'libraries/Controller.php');
require_once(APPPATH.'libraries/MY_Controller.php');
require_once(APPPATH.'controllers/ControllerA.php');

class Login extends ControllerA
{
}

Try this. Might work

El Forum

[eluser]Chillahan[/eluser]
Can anyone else provide insight on this? The MY_ extension is special, because it gets auto-loaded by the framework, right... But if one wants to have multiple "MY" controllers (like one for non-auth areas and one for, but both with the same base of default model loading, class variable loading (such as into $this->data), etc.), is there a "proper" way to do it in CI?

Does the above work, and is it possibly causing more problems down the road?

El Forum

[eluser]cideveloper[/eluser]
This might work for you. You can make as many base controllers as you want using this method.

El Forum

[eluser]Chillahan[/eluser]
I should have mentioned that I did check that out already - I was left on the fence though, because it seemed from the comments that it opened some new issues with stuff being checked in the constructor not always being called. Have you used that solution successfully? I might give it another read, just seem to remember there being some other potential issues with it.

I wonder why they couldn't just make it work with any number of MY_ controllers out of the box, rather than just the one. But it applies to the other core classes too I guess.

Any feedback you have on Phil's base classes solution would be appreciated!