Composer类映射自动加载不会在文件夹中加载新文件 [英] Composer classmap autoload does not load new files in folder
问题描述
以下问题:我在我的composer.json中定义了一个类映射:
The following problem: I have defined a classmap in my composer.json:
"autoload": {
"classmap": [
"app/controllers",
"app/models",
"app/helper.php"
]
}
但是,当我在controllers或models文件夹中创建一个新文件时,我总是要做一个作曲家dump-autoload。
However, when I create a new file in the "controllers" or "models" folder, it will not load them and I always have to make a composer dump-autoload.
这是正确的行为吗?我认为来自作曲家的自动加载器监视文件夹中的新文件然后?
Is this the correct behavior? I thought the autoloader from composer monitors the folder for new files then?
推荐答案
是的,这是正确的行为。如果你想要自动加载新类,你必须使用PSR-0或PSR-4自动加载。
Yes, this is correct behaviour. If you want new classes to be loaded automatically, you have to use either PSR-0 or PSR-4 autoloading.
生成类映射需要Composer知道包含的文件名一个类。这只能通过解析目录中的所有源代码并扫描类,接口和trait定义来完成。
Generating the classmap requires Composer to know the filename that contains a certain class. This can only be done by parsing the whole source code in the directory and scanning for classes, interfaces and trait definitions.
这通常是一个CPU和I / O密集型任务,因此只有在Composer安装/更新或(按需)转储自动加载器时才会执行,而不是每个 requirevendor / autoload.php;
This usually is a CPU and I/O intensive task, so it is only done when Composer does install/update or (on demand) dumps the autoloader, it is not done with every require "vendor/autoload.php";
.
请注意,类映射自动加载仅适用于未实现至少PSR-0的旧旧代码库。它不是为新的代码 - 除非你想支付的价格,在开发过程中一次又一次地转储自动装载机。
Note that the classmap autoloading is simply there for old legacy codebases that didn't implement at least PSR-0. It is not intended for new code - unless you want to pay the price to dump the autoloader again and again during development.
这篇关于Composer类映射自动加载不会在文件夹中加载新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!