Autoloading Files

    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, like, 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.

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

    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 . 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:

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

    Important

    Config files are namespaced in the 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.

    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.

    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 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