Laravel 5 optimization commands
The intention of this article was to explain the somewhat confusing optimization commands that shipped with Laravel prior to version 5.1. With Laravel 5.1, released in June 2015, the commands and their logic have been cleaned up, and the article has been updated to reflect the changes.
Laravel 5 ships with a set of commands that optimize the framework for better performance. This article explains what the commands actually do and where the cache files are stored.
php artisan optimize php artisan config:cache php artisan route:cache
The optimization files generated by the artisan optimization commands are written to the
The optimize command is no longer needed due to recent improvements to the PHP op-code caching. The Laravel documentation advices you to remove all references to this command from your deployment scripts as it will be removed in a future Laravel release. The following explanation of the command is kept as a reference and can be safely ignored.
php artisan optimize creates a compiled file of commonly used classes in order to reduce the amount of files that must be included on each request. The file is saved to, or overwrites,
You can specify additional classes to be included by adding them to
The compiled file is only created if the environment is production, unless the
--force flag is used.
artisan optimize also creates
bootstrap/cache/services.json which is used to optimize the loading of service providers. (The command does no longer compile views.)
php artisan clear-compiled reverses the process by deleting
NB! The ouput of
artisan optimize may depend on your configuration files, e.g. the
providers array from
config/app.php. During deployment, run this command after
php artisan config:cache.
php artisan config:cache combines all your configurations into one file for faster loading. The cache file is saved to
bootstrap/cache/config.php. The command clears the old cache before creating a new one.
php artisan config:clear reverses the process by deleting
php artisan route:cache creates a route cache file for faster route registration. The cache file is saved to
bootstrap/cache/routes.php. The command clears the old cache before creating a new one.
php artisan route:clear deletes the cache file.
Laravel compiles view files the first time they’re rendered by the framework. The compiled file is named by calculating the SHA-1 hash of its path (not its content).
artisan view:clear clears all compiled view files from
Prior to version 5.1, the optimization files generated by the artisan optimization commands were written to the
vendor/ directory by default. If the vendor directory wasn’t writable, the files were written to
storage/framework/. As of v5.0.20 you were able to set
storage/framework/ as the default storage path for optimizations by adding the following line to
Starting with version 5.1, Laravel uses a dedicated directory for optimization files,
bootstrap/cache, which contains compiled.php, routes.php, config.php, and services.json.