Ad

Laravel Autoloading Classes Within Custom Folders

- 1 answer

I have a project and need to make it so I no longer have to run composer dump-autoload when creating new classes

The project has custom folders like this App\FeaturesApp\Concerns

I've gone into composer.json, and added the last entry to the following:

"classmap": [
  "database/seeds",
  "database/factories",
  "app/Features"
],

Then I run the following commands

php artisan clear-compiledcomposer dump-autoloadphp artisan optimize

Then I went ahead and created a new class within Features called TestFeature.php, with a static function that simply prints out that it worked

I tried executing this without composer dump-autoload, but it throws an error saying the class cannot be found.

What is the best approach to autoload classes within custom folders?

Thanks

Ad

Answer

I am afraid you are using the incorrect key classmap for this. you should be using psr-4.

For example:

"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Features\\": "app/Features/"
    },
    "classmap": [ ... ]
},

Then run composer dump-auto.

P.S. putting the Features folder under app folder is a little strange; as app is already mapped to App.

Ad
source: stackoverflow.com
Ad