CodeIgniter Forums
How To Sub Class An Existing Controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How To Sub Class An Existing Controller (/showthread.php?tid=58186)



How To Sub Class An Existing Controller - El Forum - 05-22-2013

[eluser]Unknown[/eluser]
I have found lots of information on this for extending the base controller but not for extending other controllers. I already have a MY_Controller which is working great for what I need to do. My application is made up of different portals. One of the things that 2 of the portals manage are properties. The code between the 2 portals is 99% the same. I am wanting to create 2 different controllers but have the inheritance look like this

MY_Controller --> PropertyBase --> PropertyContractor

MY_Controller --> PropertyBase --> PropertyManager

Is it possible to do this?

Thanks

Paul


How To Sub Class An Existing Controller - El Forum - 05-23-2013

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums!

Yes you can, and it's rediculously easy to achieve! Just include the file you want to extend at the top of your controller:

./application/core/MY_Controller.php
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed!');

class MY_Controller extends CI_Controller {
    /* ... */
}

./application/controllers/PropertyBase.php
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed!');

class PropertyBase extends MY_Controller {
    /* ... */
}

./application/controllers/PropertyContractor.php
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed!');

include APPPATH . 'controllers/PropertyBase' . EXT; // Ta-da!

class PropertyContractor extends PropertyBase {
    /* ... */
}

Hope this helps.


How To Sub Class An Existing Controller - El Forum - 05-23-2013

[eluser]Unknown[/eluser]
Sweet! Thank you so much for the help. Worked great!

Paul