ファイルのオートローディング¶
すべてのアプリケーションは、たくさんの異なる場所に配置された膨大な数のクラスで構成されます。
このフレームワークも基幹機能のためにクラスを用意しています。あなたのアプリケーションも動作するためにたくさんのライブラリ、モデル、その他のものを持つことでしょう。プロジェクトで使用するサードパーティのクラスもあるでしょう。ひとつひとつのファイルがどこにあるかに絶えず注意をし、ファイル位置をハードコーディングした requires()
の集団はすごく頭痛の種ですし、エラーを引き起こしがちです。そこでオートローダの出番です。
CodeIgniter はとても柔軟なオートローダを提供しており、とても少ない設定で使用できます。 It can locate individual non-namespaced classes, namespaced classes that adhere to PSR4 autoloading directory 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 Composer, or even your own custom autoloaders, if needed. Because they're all registered through spl_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 the
beginning of the framework's execution.
設定¶
Initial configuration is done in /app/Config/Autoload.php. This file contains two primary arrays: one for the classmap, and one for PSR4-compatible namespaces.
名前空間¶
The recommended method for organizing your classes is to create one or more namespaces for your
application'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 directory
those classes can be found in:
$psr4 = [
'App' => APPPATH,
'CodeIgniter' => SYSTEMPATH,
];
The key of each row is the namespace itself. This does not need a trailing slash. If you use double-quotes
to 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 should
have 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 the
new namespace value under the APP_NAMESPACE
setting:
define('APP_NAMESPACE', 'App');
You will need to modify any existing files that are referencing the current namespace.
重要
Config files are namespaced in the Config
namespace, not in App\Config
as you might
expect. This allows the core system files to always be able to locate them, even when the application
namespace has changed.
Classmap¶
The classmap is used extensively by CodeIgniter to eke the last ounces of performance out of the system
by not hitting the file-system with extra is_file()
calls. You can use the classmap to link to
third-party libraries that are not namespaced:
$classmap = [
'Markdown' => APPPATH .'third_party/markdown.php'
];
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 provides a 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 at
ROOTPATH.'vendor/autoload.php'. If you need to change the location of that file for any reason, you can modify
the value defined in Config\Constants.php
.
注釈
If the same namespace is defined in both CodeIgniter and Composer, CodeIgniter's autoloader will the first one to get a chance to locate the file.