0
class Settings extends USER_Controller {...}

class USER_Controller extends MY_Controller {...}//limit access to user and define some params and functions that is user depended

class MY_Controller extends CI_Controller {...}//provide extra functionality accross all controllers

well if i try to create above 3 class's this will not work.

yet if i just use settings extends MY_Controller it will work fine.

so is there a way i can put a middle class between my controller and MY_Controller -the base controller that extends CI_Controller ?

Zalaboza
  • 8,899
  • 16
  • 77
  • 142

5 Answers5

3

As @Minhaz Ahmed said, the problem is loading the class. Codeigniter only loads the class with the subclass prefix as in,

https://github.com/EllisLab/CodeIgniter/blob/develop/system/core/CodeIgniter.php#L276

if (file_exists(APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php'))
{
    require APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php';
}

So after some diggings I have came with a solution using both codeigniter hooks and spl_​autoload_​register() for my project without hacking the CORE. Please follow the steps to achieve what's in THE QUESTION.

  1. Enable hooks in the config file if it isn't already.

  2. create a hook in application/config/hooks.php as below.

    $hook['pre_system'] = array( 'class' => '', 'function' => 'autoload', 'filename' => 'autoload.php', 'filepath' => 'hooks', 'params' => '' );

  3. Create the autoload.php in application/hooks folder.

  4. inside the autoload.php file,

    function autoload() { spl_autoload_register(function($class) { if(strpos($class,'CI_') !== 0 && file_exists(APPPATH.'core/'.$class.EXT)) { require_once APPPATH . 'core/' . $class . EXT; } }); }

That's it.

Note: Here, I've used pre_system hook, not pre_controller, since codeigniter loads the base controller classes between pre_system and pre_controller hooks.

Hope this helps. If there are any issues with this please do comments.

Ijas Ameenudeen
  • 9,069
  • 3
  • 41
  • 54
1

Place the user_controller class file at the end of MY_Controller.php

MY_Controller.php in /application/core/

class MY_Controller extends CI_Controller {...}
class USER_Controller extends MY_Controller {...}

Now from your controller in your controllers folder, you can extend the controller from USER_Controller:

 class Settings extends USER_Controller {...}
Nouphal.M
  • 6,304
  • 1
  • 17
  • 28
1

What did the best job for CI 2.1.4:
application/core

create for example:
- base_controller (base instructions, extending from ci_controller)
- admin_controller (extending from basecontroller and adds authentication instructions)


Then use them in: application/controllers:
for example:
class users extends admin_controller...

Ardian
  • 11
  • 4
0

You could use Phil's core solution, its simple and easy.

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

Mehmet Uyarovic
  • 304
  • 2
  • 9
  • Whith Phil solution, you only can define one autoload. Check http://php.net//manual/en/function.spl-autoload-register.php – Federico J. Jul 31 '14 at 15:26
  • You could extend all classes each other if you want another core class. So basicly on Object Oriented idea based on this? – Mehmet Uyarovic Aug 01 '14 at 13:37
  • Yeah, the problem is not about extending it, the problem is how your PHP file finds the classes when they're not directly included in your code via include/require. __autoload defines how to do it, but with spl_autoload_register you may define more than one function, in case it doesn't find it with autolad nor it's included – Federico J. Aug 01 '14 at 16:05
0

You can extend the class the way you want but the problem is loading the class, you have to include your USER_Controller your Settings Class. Problem is not extend problem is including the file, so you have to find a way to include the second class.

See http://www.php.net/manual/en/function.spl-autoload.php

Minhaz
  • 446
  • 5
  • 7