Welcome Guest, Not a member yet? Register   Sign In
/application/core/MY_Controller.php
#1

(This post was last modified: 11-03-2015, 08:00 AM by Rashid.)

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).
Reply
#2

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
Reply
#3

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

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'); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

Using `require` in MY_Controller would be okay.

PHP Code:
<?php

class MY_Controller extends CI_Controller {
 
   ...
}

require 
__DIR__.'/Website_Controller.php'
Reply
#6

Here's an old, but still applicable read: https://philsturgeon.uk/codeigniter/2010...ng-it-DRY/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB