Search code examples
phpcodeigniterextending

Codeigniter - extending controller does not work


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.


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