Welcome Guest, Not a member yet? Register   Sign In
Extending Application Controller

I did a search through the forums but couldn't find anything that matched what I was really looking for. I've read the user guide about extending libraries and I've extended helpers in my application already, but I was wondering if there's a way to extend an application controller.

For instance, I have a controller called main.php, but since the application I'm writing will be distributed, I wanted to give any potential developers looking to extend the system the chance to build on the controllers without actually touching them. If I update the application and they've modified the controller files, they either have to overwrite all their changes and do them over again, or they have to go through the controller line by line to see where I've changed things. To avoid this, I was hoping CI would allow me (or them rather) to extend my controllers in the same way that helpers and libraries are extended.

So, I have a controller called main.php with methods A, B, and C. Is it possible to create a file called MY_main.php that extends main and lets me add methods D and E so that when someone goes to main/d or main/e it pulls those methods from the extended class instead of having to go to MY_main/d or MY_main/e?

(I do realize I could have developers just create new routes for all these, but I was hoping there's a more transparent method of doing this.)

You can certainly extend controller classes, however the operation is the reverse of what you describe.

You define MY_main controller with methods A B C and your clients extend that with their own `main` sub-class.

This allows you modify MY_main later without affecting their extensions. (to a point)

So I'd create MY_main.php in my controllers directory and create all the methods I want to use. Does this mean that I have to create a route to redirect main to MY_main in the URL or is CI smart enough to do all that? Then, I assume the main.php would then extend MY_main and the constructor would be parent::MY_main?

You do need to require_once MY_main.php manually in your main.php controller class. CI only loads system library and helper (core) extensions automatically.

You might like to autoload the MY_main class as a library. But this will give you a redundant unused object in your application.

Yes to everything else.
require_once APPPATH.'libraries/MY_Main.php';

class Main extends MY_Main
    function Main()

MY_main is used the moment main is instantiated, the main class actually becomes part of MY_main class, so nothing needs to be redirected or routed.

And do I require MY_main in the constructor or outside the class entirely?

Just as I have shown above. Good Luck. Wink

Ahh yeah, there it is. LOL. Awesome! Thanks wired, you rock. Smile

You're Welcome. Smile

[quote author="AgentPhoenix" date="1216663780"]Thanks wired, you rock[/quote]
That sounds just too perfect - you planned that name for just stuff like this didn't you! ;-)

Theme © iAndrew 2016 - Forum software by © MyBB