58

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
AymDev
  • 6,626
  • 4
  • 29
  • 52
Ahmed Balti
  • 581
  • 1
  • 4
  • 3

1 Answers1

139

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.

AymDev
  • 6,626
  • 4
  • 29
  • 52
  • 1
    how does all the other built in module calls App\ when the folder is app\? – e-Fungus Jun 18 '21 at 16:24
  • 3
    @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](https://www.php-fig.org/psr/psr-4/). However, it is very common (I'd even say *standard*) to put all of your PHP classes in a `src` directory. – AymDev Jun 19 '21 at 10:28
  • what if i have multiple class in one folder, that time how i name the file and use that classes? – dhruvin prajapati Aug 17 '21 at 07:05
  • @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. – AymDev Aug 17 '21 at 17:12
  • In my case I was getting this error because I had a space in the filename! ie MyFilename .php. Took me ages to spot! – theotherdy May 18 '23 at 15:39