CodeIgniter Forums
Can a Controller extend another Controller? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Can a Controller extend another Controller? (/thread-4370.html)



Can a Controller extend another Controller? - El Forum - 11-20-2007

[eluser]Fabian Graßl[/eluser]
Hi!

I would like to use the same Constructor and some private functions in two different Controllers. Therefore I would like to create a Controller that is extended by two other Controllers. Is that possible?


Can a Controller extend another Controller? - El Forum - 11-20-2007

[eluser]esra[/eluser]
Do a search on BaseController in the forums and you should get numerous hits.


Can a Controller extend another Controller? - El Forum - 11-20-2007

[eluser]Michael Ekoka[/eluser]
You can create a set of base controllers for your application and store them in /application/libraries/MY_Controller.php.

Code:
class Users_Controller extends Controller{

}

class App_Controller extends Controller{

}

class Admin_Controller extends Controller{

}
Then you can just declare your other controllers in /application/controllers/ like
Code:
class Members extends Users_Controller{
}

If you need to extend one of your application's controllers instead, make sure to include the file at the top of the script like you would normally :
Code:
include APPPATH.'controllers/members.php';
class Gold extends Members{
}



Can a Controller extend another Controller? - El Forum - 11-20-2007

[eluser]Derek Allard[/eluser]
This is one of the techniques I said I liked in my review of the book "Codeigniter for Rapid PHP Application Development". If you are just starting out, you might find it helpful, but I would recommend you first start with the userguide (not implying you haven't, more just good advice Wink)


Can a Controller extend another Controller? - El Forum - 11-21-2007

[eluser]Fabian Graßl[/eluser]
Thank you very much! Smile