Laravel 5: Auto loading for new class (mapping) has been removed from laravel 5.0

I mean to record this in the past but kind a forget about it.

What happen is while upgrading my laravel system from 5.0 to another version above. I find it lost automated function to adding new module/class into the class_map.

As naive as I was, I thought it was a great feature at that time to save me not to run the “composer dump-autoload” command to import new classed into the library. Because at that time in some server I wasn’t allow to have the Shell Access and hence no idea how to run the dump-autoload command. (Can still deploy it with FTP access from online application I was using – deploybot at that time).

Then it hit me when I do a quick update on the framework. Which whenever I deploy the new code it will throw Class not found exception in Laravel.

With a bit a investigation and research here is what I found.

In Laravel 5.0, my composer.json has this section (it is not in the current archive in github anymore… I think they “fixed” it.. lol lucky me to get this bit when it still hanging around.):

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

When compare to the later version e.g. Laravel 5.1

"autoload": {
"classmap": [
"src/Illuminate/Queue/IlluminateQueueClosure.php"
],
"files": [
"src/Illuminate/Foundation/helpers.php",
"src/Illuminate/Support/helpers.php"
],
"psr-4": {
"Illuminate\\": "src/Illuminate/"
}
},

So the trick is on this bit below:

"psr-4" : {
"App\\": "app/"
}

This specific tell the composer to autoload any class within the app/ folder.

If you want new classes to be loaded automatically, you have to use either PSR-0 or PSR-4 autoloading.
By Sven (in Stackoverflow)

As the classmapping in Composer usually is a CPU and I/O intensive task, it only done when Composer does install/update or dumps the autoloader.

But I think it is still useful during the development to have the app/ folder add into the autoload requirement. But running the an composer dump-autoload is easy anyway, so it is up to you how you want to setup your environment. ** It will be good to make sure you remove it from the product …

Reference: Composer classmap autoload does not load new files in folder

Leave a Reply

Your email address will not be published.

Name *
Email *
Website