2

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.

3 Answers 3

3

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 ..

Sign up to request clarification or add additional context in comments.

3 Comments

yes but its the application/core which we are talking about here not the system/core .. check the manual
can I rename the My_Controller.php file into something like My_Form.php?
no you cants, since this is related to your Controllers, but if you want to extend CI_Form, then yes you can Call it My_Form
3

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_';

3 Comments

Yeah, I tried changing it and stuff, but it couldn't find my sub-class anyhow
This usually ends up beeing a lower/uppercase problem, your example has My with lowercase y. Maybe that's the reason. (or could be your config has My_)
Sorry about that, was a copy mistake. :) In my code it is MY_
2

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
}

2 Comments

But that wasn't mentioned in de userguide of CodeIgniter. Are you sure this is the correct way?
It works for me to, but I still doubt this is best way. Thank anyhow. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.