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.

DijkeMark
  • 1,284
  • 5
  • 19
  • 41

3 Answers3

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_';
complex857
  • 20,425
  • 6
  • 51
  • 54
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 ..

Zaher
  • 1,120
  • 7
  • 18
  • yes but its the `application/core` which we are talking about here not the `system/core` .. [check the manual](http://codeigniter.com/user_guide/general/core_classes.html) – Zaher Aug 17 '12 at 18:55
  • can I rename the My_Controller.php file into something like My_Form.php? – bonbon.langes Jul 29 '13 at 09:05
  • 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 – Zaher Jul 29 '13 at 12:53
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
}
Matt
  • 9,068
  • 12
  • 64
  • 84