1
votes

I've read all the post I found regarding this issue but nothing works. I'm using Codeigniter 2.02 in a LAMP with Apache2.2 and PHP5.3.2

I'm trying to create a common controller from which my common controllers will inherit so I can do common tasks there.

I have this:

file: parent_controller.php

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

class Parent_controller extends CI_Controller {

    public function Parent_controller()
    {
        parent::__construct();
    }

    public function index() {
        echo "Hi!";
    }
}

file: welcome.php

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

class Welcome extends Parent_controller {

    public function __construct()
    {
        parent::__construct();
    }
}

I've tried the next solutions I've found, but none of them are working:

  • public function __contstruct() instead of public function Parent_controller()

  • parent::Parent_controller();

  • put the parent_controller.php file into core

  • put the parent_controller.php file into controllers

  • adding this to config/config.php:

    function __autoload($class){
            if (file_exists(APPPATH."(controllers|core)/".$class.EXT)){
                 require_once(APPPATH.'(controllers|core)/'.$class.EXT);
           }
    }
    

Thank you all.

3
I forgot to say it, but in every case, the response html is BLANK, no errors even having define('ENVIRONMENT', 'development'); - luso

3 Answers

3
votes

Take a look at this post from Phil Sturgeon:

http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY

The key is using the native autoload as explained in his post:

/*
| -------------------------------------------------------------------
|  Native Auto-load
| -------------------------------------------------------------------
| 
| Nothing to do with cnfig/autoload.php, this allows PHP autoload to work
| for base controllers and some third-party libraries.
|
*/
function __autoload($class)
{
    if(strpos($class, 'CI_') !== 0)
    {
        @include_once( APPPATH . 'core/'. $class . EXT );
    }
}

NOTE

As a note, you'll want to put all of your "base" controllers in the core folder for CI2+

1
votes

This bit is correct

public function __contstruct() instead of public function Parent_controller()

But what you're looking for is the MY_ prefix. So if you create the controller in the /application/libraries/ folder and call the file MY_Controller.php and the class MY_Controller it'll work.

You can also change the MY_ prefix to whatever you'd like in the config.php file. Look for:

/*
|--------------------------------------------------------------------------
| Class Extension Prefix
|--------------------------------------------------------------------------
|
| This item allows you to set the filename/classname prefix when extending
| native libraries.  For more information please see the user guide:
|
| http://codeigniter.com/user_guide/general/core_classes.html
| http://codeigniter.com/user_guide/general/creating_libraries.html
|
*/
$config['subclass_prefix'] = 'MY_';

For further reading and a more depth explanation see http://codeigniter.com/user_guide/general/core_classes.html

0
votes

Also note it doesn't load a multitude of files. It simply looks for 1 controller called MY_Controller.php.

If you are thinking it will load MY_Test_Controller.php and MY_Web_Controller.php, it won't.

If you can include multiple controllers in that one file, or include other files from that file.

You could build around this of course, but a good bit of extra information to know.