Autoloading Files

Every application consists of a large number of classes in many different locations.The framework provides classes for core functionality. Your application will have anumber of libraries, models, and other entities to make it work. You might have third-partyclasses that your project is using. Keeping track of where every single file is, andhard-coding that location into your files in a series of requires() is a massiveheadache and very error-prone. That’s where autoloaders come in.

CodeIgniter provides a very flexible autoloader that can be used with very little configuration.It can locate individual non-namespaced classes, namespaced classes that adhere toPSR4 autoloadingdirectory structures, and will even attempt to locate classes in common directories (like Controllers,Models, etc).

For performance improvement, the core CodeIgniter components have been added to the classmap.

The autoloader works great by itself, but can also work with other autoloaders, likeComposer, or even your own custom autoloaders, if needed.Because they’re all registered throughspl_autoload_register,they work in sequence and don’t get in each other’s way.

The autoloader is always active, being registered with spl_autoload_register() at thebeginning of the framework’s execution.

Configuration

Initial configuration is done in /app/Config/Autoload.php. This file contains two primaryarrays: one for the classmap, and one for PSR4-compatible namespaces.

Namespaces

The recommended method for organizing your classes is to create one or more namespaces for yourapplication’s files. This is most important for any business-logic related classes, entity classes,etc. The psr4 array in the configuration file allows you to map the namespace to the directorythose classes can be found in:

  1. $psr4 = [
  2. 'App' => APPPATH,
  3. 'CodeIgniter' => SYSTEMPATH,
  4. ];

The key of each row is the namespace itself. This does not need a trailing slash. If you use double-quotesto define the array, be sure to escape the backward slash. That means that it would be My\App,not My\App. The value is the location to the directory the classes can be found in. They shouldhave a trailing slash.

By default, the application folder is namespace to the App namespace. While you are not forced to namespace the controllers,libraries, or models in the application directory, if you do, they will be found under the App namespace.You may change this namespace by editing the /app/Config/Constants.php file and setting thenew namespace value under the APP_NAMESPACE setting:

  1. define('APP_NAMESPACE', 'App');

You will need to modify any existing files that are referencing the current namespace.

Important

Config files are namespaced in the Config namespace, not in App\Config as you mightexpect. This allows the core system files to always be able to locate them, even when the applicationnamespace has changed.

Classmap

The classmap is used extensively by CodeIgniter to eke the last ounces of performance out of the systemby not hitting the file-system with extra is_file() calls. You can use the classmap to link tothird-party libraries that are not namespaced:

  1. $classmap = [
  2. 'Markdown' => APPPATH .'third_party/markdown.php'
  3. ];

The key of each row is the name of the class that you want to locate. The value is the path to locate it at.

Legacy Support

If neither of the above methods finds the class, and the class is not namespaced, the autoloader will look in the/app/Libraries and /app/Models directories to attempt to locate the files. This providesa measure to help ease the transition from previous versions.

There are no configuration options for legacy support.

Composer Support

Composer support is automatically initialized by default. By default, it looks for Composer’s autoload file atROOTPATH.’vendor/autoload.php’. If you need to change the location of that file for any reason, you can modifythe value defined in Config\Constants.php.

Note

If the same namespace is defined in both CodeIgniter and Composer, CodeIgniter’s autoloader will bethe first one to get a chance to locate the file.