I had the same problem and found the following article, which was very helpful:
https://sentinelstand.com/article/laravel-5-optimization-commands
The only solution that was working for me was manually deleting bootstrap/cache/compiled.php
. Switching around the order in which the autoloaders are called in bootstrap/autoload.php
did not work for me because I had the same problem the other way round, ie. I had a class in compiled.php
that was causing something from autoload.php
to be autoloaded before autoload.php
ran.
In my case, I am using a combination of PSR4 and manual class mappings in my composer.json
file. I'm sure this is part of the problem. (Don't judge me: this app started in Laravel 3, so it's taking time to add namespacing throughout the code base :-).
One reason why things may work differently in different environments is because the artisan optimize
command will only generate the bootstrap/cache/compiled.php
file if you provide the --force
option or if debugging mode is not enabled. So it is likely that you are not getting this file in development because debugging is enabled but are getting this file in staging and/or production because debugging is not enabled.
Ultimately, here's what I have landed on as a solution for production deployments:
artisan config:cache
artisan optimize
rm bootstrap/cache/compiled
- Update symlink to point to new version.
This way you still get bootstrap/cache/services.json
, which is helpful, whereas artisan clear-compiled
removes that file. Also, there will be a very brief period of time where bootstrap/cache/compiled.php
will exist, which is why it is important to run these commands before you update the symlink to point your web server at the new version.
It is also worth noting that the compiled.php
file that is created by artisan optimize
in Laravel 5.1 is no longer generated in Laravel 5.4 because, as Taylor has stated, PHP 7 is much more performant and so the benefit of bundling all the application classes into one file, which is meant to save on disk I/O, is negligble now. Taylor recommends enabling and properly configuring your OPcache instead - you will get far more performance benefits from that.