2

Is it possible to define multiple sub class prefix. Now the prefix is written in following format.

$config['subclass_prefix'] = "MY"; 

Here I can create custom controller MY_Controller, which is appended with prefix "MY".

Here I want to create another custom controller called MY_AdminController along with MY_Controller. How do I define my subclass prefix in this situation.

4
  • have a look at stackoverflow.com/questions/9494492/… it seems to answer your question/problem Commented Mar 7, 2014 at 11:23
  • Sorry...Not exactly what I am looking for Commented Mar 7, 2014 at 11:39
  • You want to extend MY_AdminController from MY_Controller ? Or trying to extend the base controller multiple times ? Commented Mar 7, 2014 at 13:02
  • No I wanted to have two custom controller called MY_AdminController and MY_Controller and should be able to derive from any one of these two Commented Mar 7, 2014 at 13:04

4 Answers 4

1

It´s too late but the right answer for me was to extend the CI_controller or MY_controller class, as you want, into the same file.. (sorry about my english)

application/core/MY_controller.php

<?php

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

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

class Admin_Controller extends CI_Controller{
   ...
   do what you need
   ...
}

?>

I saw this answer in another post (can´t remember) and I solved my problem

Sign up to request clarification or add additional context in comments.

Comments

1

I have some tricks. I set a new configuration 'loading-cores' on config.php. You can set the config name whatever you want.

$config['loading-cores'] = array(
    "load-my-cores" => TRUE,
    /* core class(es) to be loaded, note: must be a php file type */
    "my-cores" => ["APIResponder", "Anotherclass"]
);

my classes in core folder:

  • APIResponder.php
  • Anotherclass.php

Then open file ..system/core/CodeIgniter.php, try to find this snippet code:

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

Add lines of codes below to load your core classes, place it right after the snippet code above:

if(isset($CFG->config['loading-cores'])){
    if($CFG->config['loading-cores']['load-my-cores'] && count($CFG->config['loading-cores']['my-cores']) > 0){
        foreach ($$CFG->config['loading-cores']['my-cores'] as $core) {
            if (file_exists(APPPATH."core/{$core}.php")){
                require_once APPPATH."core/{$core}.php";
            }
        }
    }
}

Set "load-my-cores" => FALSE if you don't need the core classes to be loaded.

Comments

0

You can only have one prefix.

What you're trying to do sounds like extending, You wish to extend an existing controller to do other stuff.

Don't forget that a controller is not just an abstract element, it's part of the MVC framework you're using.

the 'base' controller should not be specific(for example, adminController), it should be as general as possible and do 'controller' stuff(load views, load all your models etc.)

Let's say you have a users controller, an admin is a type of user, so it should extend the users model. This prevents you from doubling your code as admins should do at least some of the stuff users are doing. Just an example.

Comments

0

Short answer: not without tweaking the core

Long answer: you can hack your way around by adding a hook in /system/core/CodeIgniter.php, which will load your new controller before any others are loaded. Here's how I've done it:

(note that I use MY_Controller for the admin controllers and MY_Basecontroller for the regular ones):

  1. Step one - you add this line to /system/core/CodeIgniter.php, line 241 - see exactly where:

    $EXT->_call_hook('pre_local_controller');
    

    You must do this just before loading the local controller, otherwise it will not work.

  2. Step two - add the following code to /application/config/hooks.php:

    $hook['pre_local_controller'][] = array(
                             'class'    => 'Hooks',
                             'function' => 'loadCore',
                             'filename' => 'Hooks.php',
                             'filepath' => 'hooks',
                             'params'   => 'MY_Basecontroller.php'
    );
    
  3. Step three - create a file /application/hooks/Hooks.php and put the following code inside:

    class Hooks
    {
        public function loadCore($class)
        {
            require_once(APPPATH . 'core/' . $class);
        }
    }
    
  4. Step four - profit

Now your classes can extend from the class MY_Basecontroller which resides in /application/core/MY_Basecontroller.php. Obviously, don't forget it should extend CI_Controller

It's still not exactly 'multiple subclass prefix' - actually it doesn't have anything to do with it, but will do what you asked for.

Tweak to your liking and go wild.

Cheers.

8 Comments

I did the same what you have mentioned above, but unfortunately I am getting this error, Fatal error: Class 'MY_Basecontroller' not found
Which CodeIgniter version are you using? Also, in what file and line are you getting this error?
Version 2.1.4 I am getting error at class declaration area in Application controller, class User extends MY_Basecontroller {}
Check /application/config/config.php for $config['enable_hooks']. Change it to true if it is not. If it is, put a die(); statement in the loadCore() method from the Hooks class to see if it gets called.
$config['enable_hooks'] = TRUE, and not getting called loadCore() method.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.