类不符合 psr-4 自动加载标准。跳

2022-08-30 11:38:07

我尝试使用作曲家自动加载,但我收到此错误

composer.json

{
    "autoload":{
        "psr-4":{
            "App\\":"app/"
        },
        "files": ["app/functions/helper.php"]
    },
    "require": {
        "vlucas/phpdotenv": "^2.4",
        "altorouter/altorouter": "^1.2",
        "philo/laravel-blade": "^3.1"
    },
    "config":{
        "optimize-autoloader":true
    }
}

我的终端输出

Generating optimized autoload files
Class App\Controllers\BaseController located in D:/php/Xamp/htdocs/MVC_PHP/app\controllers\BaseController.php does not comply with psr-4 autoloading standard. Skipping.
Class App\Controllers\IndexControllers located in D:/php/Xamp/htdocs/MVC_PHP/app\controllers\IndexControllers.php does not comply with psr-4 autoloading standard. Skipping.
Class App\RoutingDispatcher located in D:/php/Xamp/htdocs/MVC_PHP/app\routing\RoutingDispatcher.php does not comply with psr-4 autoloading standard. Skipping.
Generated optimized autoload files containing 508 classes

答案 1

PSR-4 标准要求文件和目录区分大小写,并且相应的类和命名空间位于 PascalCase 中。

对于类,该文件应位于:App\Controllers\BaseController

app/Controllers/BaseController.php

请注意大写字母C

第二个错误:对于顶级命名空间之后的任何命名空间,必须有一个同名的目录。
您有一个应放置为的类,但该文件将对应于一个类。
您必须更改该类的命名空间或移动该文件。App\RoutingDispatcherapp/RoutingDispatcher.phpapp/routing/RoutingDispatcher.phpApp\Routing\RoutingDispatcher

如果更改其命名空间,请确保使用前导大写字母重命名目录。app/routing


答案 2

推荐