CodeIgniter Forums
/application/core/MY_Controller.php - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: /application/core/MY_Controller.php (/showthread.php?tid=63470)



/application/core/MY_Controller.php - Rashid - 11-03-2015

Hello, what if I need two (or more) different MY_Controller classes in a core folder? CodeIgniter allows creation only one "extended" version? Sad

I tried
PHP Code:
class MY_AuthController extends CI_Controller
{
    public function 
__construct()
    {
        
parent::__construct();
        
/* ... */        
    
}

but that didn't work (conventional MY_Controller works fine).


RE: /application/core/MY_Controller.php - Martin7483 - 11-03-2015

You just create a single MY_Controller.php file and place your required classes in this file.

I have a MY_Controller.php which has a WEBSITE_Controller class and a ADMIN_Controller class


RE: /application/core/MY_Controller.php - orionstar - 11-03-2015

You should create an autoload function (spl_autoload_register) which can load any base controller.


RE: /application/core/MY_Controller.php - InsiteFX - 11-03-2015

Here is the autoload method:

PHP Code:
/*
|--------------------------------------------------------------------------
| Autoloader function
|--------------------------------------------------------------------------
|
| Add to the bottom of your ./application/config/config.php file.
|
| @author Brendan Rehman
| @param $class_name
| @return void
*/
function __autoloader($class_name)
{
    
// class directories
    
$directories = array(
        
APPPATH 'core/',
        
// add more autoloading folders here… and you’re done.
    
);

    
// for each directory
    
foreach ($directories as $directory)
    {
        
// see if the file exsists
        
if (file_exists($directory.$class_name '.php'))
        {
            require_once(
$directory.$class_name '.php');
            
// only require the class once, so quit after to save effort (if you got more, then name them something else

            
return;
        }
    }
}

spl_autoload_register('__autoloader'); 



RE: /application/core/MY_Controller.php - kenjis - 11-05-2015

Using `require` in MY_Controller would be okay.

PHP Code:
<?php

class MY_Controller extends CI_Controller {
 
   ...
}

require 
__DIR__.'/Website_Controller.php'



RE: /application/core/MY_Controller.php - CroNiX - 11-10-2015

Here's an old, but still applicable read: https://philsturgeon.uk/codeigniter/2010/02/08/CodeIgniter-Base-Classes-Keeping-it-DRY/