Composer 类映射自动加载不会在文件夹中加载新文件 [英] Composer classmap autoload does not load new files in folder

查看:35
本文介绍了Composer 类映射自动加载不会在文件夹中加载新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下问题:我在我的 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"文件夹中创建一个新文件时,它不会加载它们,我总是必须制作一个composer 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.

这是正确的行为吗?我认为 Composer 的自动加载器会监视文件夹中是否有新文件?

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 知道包含某个类的文件名.这只能通过解析目录中的整个源代码并扫描类、接口和特征定义来完成.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆