CodeIgniter Forums
extending base controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: extending base controller (/showthread.php?tid=20562)

Pages: 1 2


extending base controller - El Forum - 07-14-2009

[eluser]ommy[/eluser]
Hi people,
I've been wondering. Since it is possible to autoload a model, one can create a base model and let other models inherit its methods by extending these models.
I was wondering if this could also be achieved with a controller. The autoload doesnt provide a way to do this so i started looking on the forum but i didnt find the answer i was looking for.

So: in short;
I want to create a controller that's always loaded in codeigniter so i can extend any other controller off of that one. That would greatly decrease the amount of copy pasting i have to do Smile

Thanks for the advice in advance!
Regards,
Tommy


extending base controller - El Forum - 07-14-2009

[eluser]Johan André[/eluser]
Well, create a MY_Controller.php in library-folder. Then let your controllers extend MY_Controller.
I actually use a frontend and backend controller that extends the MY_Controller-class. Works like a charm.

And I place the Frontend and Backend-class in the same file as MY_Controller.php (since it's loaded automatically). Don't know if thats the best way to do it though...


extending base controller - El Forum - 07-15-2009

[eluser]ommy[/eluser]
sounds like a pretty solid way. I'll let you know how that works out for me Smile


extending base controller - El Forum - 07-15-2009

[eluser]Johan André[/eluser]
[quote author="ommy" date="1247657966"]sounds like a pretty solid way. I'll let you know how that works out for me Smile[/quote]

oki! let me know!
I'm thinking of doing a screencast series of some more "advanced" Codeigniter stuff and tips.
This topic should probably be covered in it... I just need to get some free time to record it...


extending base controller - El Forum - 07-15-2009

[eluser]ommy[/eluser]
Nice tip.
Works like a charm idd.

Had a little trouble resolving a totally unrelated matter so it took me a few minutes longer but now it works.
Seems great for controllers that need inheritance. Seems a little incoherent to me that models can be loaded through the models folder / autoload cfg and controllers can't. But once you know tricks this handy it doesnt really matter anymore.

What i'm trying to say is;
Thanks for the advice Johan!

If anyone else knows a better way?


extending base controller - El Forum - 09-15-2009

[eluser]vecima[/eluser]
sorry for dragging up this old post, but is there any reason why your base controller needs to be in the application/libraries folder?

I have a Base_Controller that extends Controller, and it's in my application/controllers folder. all of my other controllers extend Base_Controller and it works fine for me.

am I doing something wrong?


extending base controller - El Forum - 09-15-2009

[eluser]ommy[/eluser]
i believe i needed a controller that i could use from a view. since it was an action that was already fully coded in an existing controller i wanted to reuse the existing functions rather than rewriting it. in the mean time i believe that ive found a more solid way to achieve this
to answer your question: i think you're doing nothing wrong. and btw: in my humble opinion; if it works its a good way even if its not the best practice Smile
i could be wrong about my original purpose. it has been a while
regards
tommy


extending base controller - El Forum - 09-15-2009

[eluser]jedd[/eluser]
[quote author="vecima" date="1253050737"]
... it's in my application/controllers folder. all of my other controllers extend Base_Controller ...
[/quote]

Out of curiosity, how do your other controllers extend your Base_Controller?


extending base controller - El Forum - 09-15-2009

[eluser]vecima[/eluser]
Code:
<?php

require_once(APPPATH.'controllers/base_controller.php');

class Articles extends Base_Controller {


    function Articles()
    {
        parent::Base_Controller();
        
        ...
    }
    
    ...
}

do you see a problem?


extending base controller - El Forum - 09-15-2009

[eluser]Greg Aker[/eluser]
If you put it in the libraries folder, call it MY_Controller, there's no need for the require_once Wink