GRY
GRY

Reputation: 724

Extend a controller Codeigniter

I have installed Codeigniter on IIS. Everything is going great.

I am trying to extend the CI_Controller class. The reason I am doing this is to put a security check in the constructor. Some controllers will extend the CI_Controller. Some controllers, which need to be secure will extend MY_Controller. This seems like a good approach to me.

So far I have followed these instructions:

Creating Core Classes

But when I call a page which extends MY_Controller I get a blank page. Check the network tab in Firebug, I see “500 internal server error.” I check my CI logs I see this:

DEBUG - 2014-11-24 10:26:47 --> Config Class Initialized
DEBUG - 2014-11-24 10:26:47 --> Hooks Class Initialized
DEBUG - 2014-11-24 10:26:47 --> Utf8 Class Initialized
DEBUG - 2014-11-24 10:26:47 --> UTF-8 Support Enabled
DEBUG - 2014-11-24 10:26:47 --> URI Class Initialized
DEBUG - 2014-11-24 10:26:47 --> Router Class Initialized
DEBUG - 2014-11-24 10:26:47 --> Output Class Initialized
DEBUG - 2014-11-24 10:26:47 --> Security Class Initialized
DEBUG - 2014-11-24 10:26:47 --> Input Class Initialized
DEBUG - 2014-11-24 10:26:47 --> Global POST and COOKIE data sanitized
DEBUG - 2014-11-24 10:26:47 --> Language Class Initialized

Now, for a normal point of reference, when I load any controller which does not extend MY_Controller I see this:

DEBUG - 2014-11-24 11:00:14 --> Config Class Initialized
DEBUG - 2014-11-24 11:00:14 --> Hooks Class Initialized
DEBUG - 2014-11-24 11:00:14 --> Utf8 Class Initialized
DEBUG - 2014-11-24 11:00:14 --> UTF-8 Support Enabled
DEBUG - 2014-11-24 11:00:14 --> URI Class Initialized
DEBUG - 2014-11-24 11:00:14 --> Router Class Initialized
DEBUG - 2014-11-24 11:00:14 --> No URI present. Default controller set.
DEBUG - 2014-11-24 11:00:14 --> Output Class Initialized
DEBUG - 2014-11-24 11:00:14 --> Security Class Initialized
DEBUG - 2014-11-24 11:00:14 --> Input Class Initialized
DEBUG - 2014-11-24 11:00:14 --> Global POST and COOKIE data sanitized
DEBUG - 2014-11-24 11:00:14 --> Language Class Initialized
DEBUG - 2014-11-24 11:00:14 --> Loader Class Initialized
DEBUG - 2014-11-24 11:00:14 --> Helper loaded: url_helper
DEBUG - 2014-11-24 11:00:14 --> Session Class Initialized
DEBUG - 2014-11-24 11:00:14 --> Helper loaded: string_helper
DEBUG - 2014-11-24 11:00:14 --> Session routines successfully run
DEBUG - 2014-11-24 11:00:14 --> Controller Class Initialized
DEBUG - 2014-11-24 11:00:14 --> Database Driver Class Initialized
DEBUG - 2014-11-24 11:00:14 --> Model Class Initialized
DEBUG - 2014-11-24 11:00:14 --> Model Class Initialized
DEBUG - 2014-11-24 11:00:14 --> File loaded: application/views/user/session_box.php
DEBUG - 2014-11-24 11:00:14 --> File loaded: application/views/public/public_menu.php
DEBUG - 2014-11-24 11:00:14 --> File loaded: application/views/public/welcome_message.php
DEBUG - 2014-11-24 11:00:14 --> File loaded: application/views/template.php
DEBUG - 2014-11-24 11:00:14 --> Final output sent to browser
DEBUG - 2014-11-24 11:00:14 --> Total execution time: 0.2295

Can anyone tell me what is wrong?

MY_Controller

class MY_Controller extends CI_Controller{

    private static $instance;

    /**
     * Constructor
     */
        function __construct()
        {
            parent::__construct();
        }

    public static function &get_instance()
    {
        return self::$instance;
    }
}

The controller which extends it:

class Project extends MY_Controller{

    public function __construct()
        {
            parent::__construct();
            $this->load->database();        
            $this->load->model('user_model');
        }

    public function index()
    {
            var_dump("hi");
        }


}

Upvotes: 0

Views: 1752

Answers (1)

Rwd
Rwd

Reputation: 35200

Change your MY_Controller to:

<?php

class MY_Controller extends CI_Controller
{

    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct();
    }

}

This is because $this will already contain the instance as it is extending CI_Controller

I can't be certain this is the issue but I hope it helps!

Upvotes: 1

Related Questions