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

[eluser]HooJee[/eluser]
Hi Guys

I have created an extension of the Controller class called Parent_Controller, but everytime I try to access it via another controller I keep getting a 'Parent_Controller not found'. Code below:

Code:
class Parent_Controller extends Controller {
    function Parent_Controller() {
        parent::Controller();
        $this->_check_session();
    }
    
    function _check_session() {
    }    
}

Code:
class Projects extends Parent_Controller {
    
    function Projects() {
        parent::Parent_Controller();
        // LOAD MODEL
        $this->load->model('Projectsmodel');
    }
}

Code:
Fatal error: Class 'Parent_Controller' not found in C:\wamp\www\personal\system\application\controllers\admin\home.php on line 4
#2

[eluser]Dam1an[/eluser]
You'll need to include it manually (using PHP include)
You could always create a controller called MY_Controller and use that as the parent, then it's loaded for you

See extending core classes
#3

[eluser]skunkbad[/eluser]
[quote author="HooJee" date="1249536513"]Hi Guys

I have created an extension of the Controller class called Parent_Controller, but everytime I try to access it via another controller I keep getting a 'Parent_Controller not found'. Code below:

Code:
class Parent_Controller extends Controller {
    function Parent_Controller() {
        parent::Controller();
        $this->_check_session();
    }
    
    function _check_session() {
    }    
}

Code:
class Projects extends Parent_Controller {
    
    function Projects() {
        parent::Parent_Controller();
        // LOAD MODEL
        $this->load->model('Projectsmodel');
    }
}

Code:
Fatal error: Class 'Parent_Controller' not found in C:\wamp\www\personal\system\application\controllers\admin\home.php on line 4
[/quote]

Based on your code, I'd think you are probably trying to go about doing something the wrong way. Why would you think you need to extend Controller? The reason to extend a controller is to modify the extended controller without physically touching its code.

So for instance, within the Controller class (which extends CI_Base), is a method called _ci_initialize(). If you wanted to change something about that method, you would place a _ci_initialize method inside your MY_Controller class, and viola!

If you want to do something like load a model everytime a request is run through CI, you should perhaps autoload it in your config/autoload.php file. Also, if you want to run some custom php every time a request is run through CI, then use a hook.

More info on hooks is available here: http://ellislab.com/codeigniter/user-gui...hooks.html

Don't make your life hard... just read the docs!




Theme © iAndrew 2016 - Forum software by © MyBB