A while ago, I was facing my worst nightmare for two straight days. It was no joke (I was even working on it in my sleep).
Below were the issues I was facing, that made me dig deeper into the topic:
- Whenever I edited the config/app.php file, it did nothing. It was as if the file didn’t exist. Even when I temporarily deleted the file, everything still worked fine!
- Route just was not responding to changes, even after adding new lines of code to the file.
Laravel 5 Artisan optimization – A brief introduction…
If you’re a bit familiar with Laravel, you’d know that Laravel optimization doesn’t really need much introduction as it is self-explanatory:
Laravel comes with a set of artisan commands that optimize the framework for better performance.
This article explains what the commands actually do and where the cache files are stored.
These are the commands that we will be focusing on:
- php artisan optimize
- php artisan config:cache
- php artisan route:cache
The optimization files generated by artisan optimization are stored in the bootstrap/cache/ directory.
1. php artisan optimize
php artisan optimize creates a compiled file of commonly used classes in other to reduce the amount of files that must be included on each request.
After running the command, all commonly used classes are compiled and then saved to this file directory: bootstrap/cache/compiled.php.
You can specify additional classes to be included by adding them to config/compile.php.
The compiled file is only created in production stage, not unless this attribute is added –force (that is, php artisan optimize –force).
2. php artisan config:cache
php artisan config:cache combines all configuration (config) files into one file for quick loading.
This cache file is stored in bootstrap/cache/config.php
Use php artisan config:clear to undo the command.
3. php artisan route:cache
php artisan route:cache creates a cache file located in the Bootstrap/cache/routes directory for faster loading. It basically registers all routes for easy access.
After running the above command, the app will use the cached file instead of the original route.php file.
Use php artisan route:clear to delete the cache files.
In addition to the commands mentioned above, here is one I’d like to add:
4. php artisan view:clear
This command is used to clear all view (resource/view/) cached files which are generated whenever php artisan optimize command is executed.
The cached files are stored in the vendor/ directory by default, if the directory is writable. If not, it’s stored in the storage/framework/ directory.
Thanks for reading thus far! I do hope you’ve found this useful.
As for those nightmares of mine, I was able to fix them after understanding how these commands work.