Welcome Guest, Not a member yet? Register   Sign In
How To Sub Class An Existing Controller
#1

[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
#2

[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.
#3

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

Paul




Theme © iAndrew 2016 - Forum software by © MyBB