class does not comply with psr-4 autoloading standard. Skipping

Ahmed Balti picture Ahmed Balti · Nov 27, 2020 · Viewed 31.7k times · Source

I try to use composer autoload but I get this error

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

my terminal output

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

Answer

AymDev picture AymDev · Nov 27, 2020

The PSR-4 standard requires your files and directories to be case sensitive and the corresponding classes and namespaces to be in PascalCase.

For a App\Controllers\BaseController class, the file should be located in:

app/Controllers/BaseController.php

Notice the uppercase C

2nd error: For any namespace after the top level namespace, there must be a directory with the same name.
You have a App\RoutingDispatcher class that should be placed as app/RoutingDispatcher.php but the app/routing/RoutingDispatcher.php file would correspond to a App\Routing\RoutingDispatcher class.
You must change the namespace of that class or move the file.

If you change its namespace, be sure to rename the app/routing directory with a leading uppercase letter.