[eluser]Nir Gavish[/eluser]
hi all!
is there anyone who knows of an easy, elegant way for one controller to inherit (extend) another existing controller?
Thanks, Nir
[eluser]Colin Williams[/eluser]
I've still yet to come across a situation that calls for this. Most the time things can be engineered a different way. Alas, search for "HMVC" here and you'll find plenty, including a Library (or is it a hack) that turns CI's MVC to HMVC.
But, honestly, I like MVC because it's just M. V. C. and it's usually enough.
[eluser]Nir Gavish[/eluser]
Thank you for your reply.
hmvc seems like too much.
all i need is a way to declare a controller:
* class New_controller extends Old_controller
instead of:
* class New_controller extends Controller
and doing so without an ugly include OR hacking into the controller class
the reason for this is that i have a management interfce controller *that is already running* that needs to be 'delegated' to other controllers without a major rewrite.
Thanks, Nir
[eluser]Sam Dark[/eluser]
Include is not ugly in this case, I think. Else you need to write custom loaders.
[eluser]Nir Gavish[/eluser]
[quote author="Sam Dark" date="1214926833"]Include is not ugly in this case.[/quote] >> well... ok... you win, i'll use include, cheers :-) and thanks
[eluser]louis w[/eluser]
Agree, just a basic include works great for this. No sense adding extra complicated work which an include (or better yet require_once) can do.
I often create subclasses. This is a great way to create a very flexible application with minimal code duplication.
[eluser]AgentPhoenix[/eluser]
I don't think you can actually have your controllers inherit anything else since they already inherit Controller and multiple inheritance isn't allowed in PHP (as far as I know, which could easily be mistaken). Nothing wrong with the simple solution.

[eluser]louis w[/eluser]
Why wouldn't they be able to inherit? Just keep subclassing. Simple example:
Code:
/* Admin.php */
class Admin extends Controller {
var $say_hi = 'Hello';
function __construct() {
parent::__construct();
}
/* Methods */
}
/* - - - - - - - - - - - - - - - - - - - - - - - */
/* Page.php */
require_once('Admin.php');
class Page extends Admin {
function __construct() {
parent::__construct();
}
/* Methods */
function test() {
echo $this->say_hi;
}
}
When instantiated, the Page controller will have access to methods and variables set inside both Admin and Controller (CI). I often do soemthing like this where Admin would load all the libraries, configs, etc, and then page just loads what it needs.
You can also do this with models. I have one model called CRUD which contains all the db logic methods and then subclass it and add table specific info and have a fully featured crud model with like 4 lines of code.
[eluser]Henrik Pejer[/eluser]
One can easily create a MY_Controller in the application/libraries section and then have all your controllers extend MY_Controller instead of Controller.
After all, that's what the MY_xxx functionality of CI is there for: to let you easily extend the core classes and CI will load them autmaticly.
Look here:
http://ellislab.com/codeigniter/user-gui...asses.html
At the bottom of the page you even have an example with the default 'welcome' controller base on an extended controller...
So.... just a little bit of RTFM and you'll get there

[eluser]Nir Gavish[/eluser]
[quote author="Henrik Pejer" date="1214965039"]One can easily create a MY_Controller in the application/libraries section[/quote]
Yes, but i was trying to avoid moving the controller into the library dir.
***
anyway, as far as RTFM goes, i mostly agree - however ATFF(Ask The F&%#ing Forum) works nicely as well, and you get to meet people as a bonus.
i finally used a require_once statement and extended the class, worked like a charm, i'm happy.
thanks, guys