6

I have created two controllers, the Public_Controller and the Admin_Controller inside ./application/libraries folder, following Phil's Sturgeon example.

What I want to do is to autoload the Public_Controller and Admin_Controller specificly, so I created this autoload function inside ./application/config.php

function __autoload($class) {

    // Autoload only Public_Controller and Admin_Controller
    if (strpos($class, 'CI_') !== 0) {
        $file = APPPATH . 'libraries/'. $class .'.php';
        if ( file_exists($file) && is_file($file) ) {
            @include_once($file);
        }
    }
}

The problem with this I think is that I have more files included inside the libraries folder, so those too are autoloaded, which is not what I want. So instead I tried to do a small change to the first if statement, like this:

if ( in_array($class, array('Public_Controller, Admin_Controller')) ) // instead of strpos

in order to target only these two classes, but this does not seem to work. Any ideas what I might doing wrong?

ltdev
  • 4,037
  • 20
  • 69
  • 129

5 Answers5

6

Go to applications/config/autoload.php and in there you can edit what you need.

They are in arrays and seperated by packages, libraries, helpers, config, languages and models.

eg

$autoload['libraries'] = array('database', 'session');

$autoload['helper'] = array('url', 'html', 'form');
Albzi
  • 15,431
  • 6
  • 46
  • 63
1

I only wanted to auto load Public_Controller in the frontend and Admin_Controller in admin, so autoload.php is out. In autoload.php the files are loaded globally. The __autoload() function only tries to auto load a class when it's called, but not found.

Albzi
  • 15,431
  • 6
  • 46
  • 63
ltdev
  • 4,037
  • 20
  • 69
  • 129
  • 1
    Then you can do this: `$this->load->model('Model_name');` inside of the controller you want. – Albzi Jul 08 '14 at 14:54
0

there is no need to write autoload function codeigniter has its own file for auo loading files like libraries and helper

You can add class name there in the specific array

The file name should be "autoload.php" in application/config/ directory

Snehal S
  • 865
  • 4
  • 13
-1

U dont need to write the autoload function ..codeiniter has an ibuilt dunction for autoloading specific files ...

go to applications/config/autoload.php ther u can add your specific file in the array

$autoload['libraries'] = array('database', 'session','your-specific_file');
Mohan
  • 4,677
  • 7
  • 42
  • 65
  • $autoload['libraries'] = array('database', 'session','folder/your-specific_file'); can we do like this? – Angel Feb 01 '16 at 06:32
  • autoload assumes that you have put your custom library in the library folder provided by Codeigniter. – Mohan Feb 01 '16 at 07:03
-1

You can use your method. __autoload() will NOT load other classes automatically. Because, according to the PHP doc,

__autoload() — Attempt to load undefined class. Your class files will be included "automatically" when you call (init) them without these functions: "include, include_once, require, require_once".

So no need to worry about that other classes will automatically.

OR

You can use the Codeigniter's in built autoload feature as

open the application/config/autoload.php file and add the item you want loaded to the autoload array. You'll find instructions in that file corresponding to each type of item. - Codeigniter Docs

Hope this helps :)

Ijas Ameenudeen
  • 9,069
  • 3
  • 41
  • 54
  • If the Downvoter can explain why this is down voted, It will be more clear for others also !! – Ijas Ameenudeen Jul 10 '14 at 02:19
  • I have problems with _"You'll find instructions in that file corresponding to each type of item."_ I need an autoloader for controllers, which isn't supported. – Syrlia Jan 08 '19 at 16:32