Question

I am trying to extend a controller with my own class which extends the default CI_Controller class. Except it doesn't work.

It says it can't find my sub-class. My subclass is located in application/core and is named My_Control_Panel.

My class that extends on my sub-class:

if (!defined('BASEPATH')) exit('No direct script access allowed');

class Developers extends My_Control_Panel
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->database();

        $this->checkIfLoggedIn();
        $this->checkIfAllowedToViewPage();
}

My sub-class:

if (!defined('BASEPATH')) exit('No direct script access allowed');

class My_Control_Panel extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }
}

It keeps saying it can't find my sub-class, while it should work.

Was it helpful?

Solution

you should name your file like this My_Controller.php inside your core folder and then you type your code like

if (!defined('BASEPATH')) exit('No direct script access allowed');

class My_Control_Panel extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }
}

and this is the right way to do it in CodeIgniter, not as mentioned in the first answer with the include one ..

OTHER TIPS

If you want CI to pick up your extended class you will have to name it MY_Controller. The MY_ part is configurable, but the other parts are not.

The MY_ part comes form the config/config.php:

$config['subclass_prefix'] = 'MY_';

You'll need to include the parent class (My_Control_Panel) in the subclass (Developers), like so:

if (!defined('BASEPATH')) exit('No direct script access allowed');

include_once '../path/to/mycontrolpanel.php';

class Developers extends My_Control_Panel
{
    // whatever
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top