Laravel Autoloading Classes Within Custom Folders
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\Features
App\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-compiled
composer dump-autoload
php 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
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
.
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS - Conditionally Load a Different Page
- → Make a Laravel collection into angular array (octobercms)
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
- → Validating fileupload(image Dimensions) in Backend Octobercms
- → OctoberCMS Fileupload completely destroys my backend
- → How do I call the value from another backed page form and use it on a component in OctoberCms