class does not comply with psr-4 autoloading standard. Skipping [duplicate]
Asked Answered
U

1

67

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
Uruguay answered 27/11, 2020 at 13:59 Comment(0)
F
158

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.

Forsworn answered 27/11, 2020 at 14:6 Comment(5)
how does all the other built in module calls App\ when the folder is app\?Winsor
@FrankChen the standard requires you to have a top level namespace corresponding to a root directory, this is what you configure in your composer.json in autoload.psr4: "Some\\TopLevel\\Namespace": "some/root-directory", both of those parts don't need to be named the same as you can see in the section 3 of the document. However, it is very common (I'd even say standard) to put all of your PHP classes in a src directory.Forsworn
what if i have multiple class in one folder, that time how i name the file and use that classes?Anastassia
@dhruvinprajapati I don't see anything specific about your question, this is the basic "setup" (a directory containing multiple class files) so just follow the standard.Forsworn
In my case I was getting this error because I had a space in the filename! ie MyFilename .php. Took me ages to spot!Dissenter

© 2022 - 2024 — McMap. All rights reserved.