Search code examples
phpcodeigniterconstructorextend

How can I get the __construct() in MY_Controller to run?


Possible Duplicate:
Codeigniter extending extended MY_Controller

I have been fiddling with this problem for a while now, and am at the point of frustration. I am extending the controller class with MY_Controller.

Here are the details:

  • __construct() does not seem to be called.
  • The file is located in application/core as it should be
  • The file is named MY_Controller.php
  • I have MY_ set up as the prefix for extensions (I have other extensions which work fine)

Here is my code:

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

class MY_Controller extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        echo 'TESTING!!!'; // This should print, followed by an ob_start() error
    }

}

I should get a page back that says TESTING!!! followed by an ob_start() error, but instead the page renders normally.

I have tried splitting up the constructor class and just calling a private function within MY_Controller, but that doesn't work. I know the file is being called because if I purposefully create a php error within MY_Controller.php, the page will error out. It seems to me that the __construct() is just not running.


Solution

  • Can you show an example of one your controllers under application/controllers? Pls note that the controllers under application/controller should extend MY_Controller instead of CI_Controller - I think this is what you missed.

    For example, under your controller application/controller/test.php. it should look like this:

    class Test extends MY_Controller {        
        public function index()
        {
             echo 'test';
        }
    }