Laravel 5 optimization commands

May 11, 2015 Updated April 8, 2017

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.

1
2
3
php artisan optimize
php artisan config:cache
php artisan route:cache

Storage paths for optimizations

The optimization files generated by the artisan optimization commands are written to the bootstrap/cache/ directory.

artisan optimize

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, 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 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 bootstrap/cache/compiled.php and bootstrap/cache/services.json.

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.

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 bootstrap/cache/config.php.

artisan route:cache

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.

artisan view:clear

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 storage/framework/views/.

Laravel 5.0

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 bootstrap/app.php:

1
$app->useStoragePathForOptimizations(true);

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.