In Laravel there are different caching mechanisms in place that you may need to flush from time to time.
Here’s a breakdown of each and what they do:
php artisan cache:clear
Re-optimize Class Loader
php artisan optimize
php artisan view:clear
Config caching The laravel config spreads across dozens of files, and including every one of them for each request is a costly process. To combine all of your config files into one, use:
php artisan config:cache
Keep in mind that any changes to the config will not have any effect once you cache it. To refresh the config cache, run the above command again. In case you want to completely get rid of the config cache, run
php artisan config:clear
Routes caching Routing is also an expensive task in laravel. To cache the routes.php file run the below command:
php artisan route:cache
Mind that it doesn’t work with closures. In case you’re using closures this is a great chance to move them into a controller, as the artisan command will throw an exception when trying to compile routes that are bound to closures instead of proper controller methods. In the same as the config cache, any changes to routes.php will not have any effect anymore. To refresh the cache, run the above command everytime you do a change to the routes file. To completely get rid of the route cache, run the below command:
php artisan route:clear
It’s not uncommon for a medium-sized project to be spread across hundreds of PHP files. As good coding behaviours dictate us, everything has its own file. This, of course, does not come without drawbacks. Laravel has to include dozens of different files for each request, which is a costly thing to do.
The additional compiles files (again: service providers, middlewares and so on) should be declared by you in config/compile.php, in the files key. Once you put there everything essential for every request made to your app, concatenate them in one file with:
php artisan optimize --force
Optimizing the composer autoload
This one is not only for laravel, but for any application that’s making use of composer.
I’ll explain first how the PSR-4 autoload works, and then I’ll show you what command you should run to optimize it. If you’re not interested in knowing how composer works, I recommend you jumping directly to the console command.
When you ask composer for the
App\Controllers\AuthController class, it first searches for a direct association in the classmap. The classmap is an array with 1-to-1 associations of classes and files. Since, of course, you did not manually add the Login class and its associated file to the classmap, composer will move on and search in the namespaces. Because App is a PSR-4 namespace, which comes by default with Laravel and it’s associated to the
app/ folder, composer will try converting the PSR-4 class name to a filename with basic string manipulation procedures. In the end, it guesses that
App\Controllers\AuthController must be located in an AuthController.php file, which is in a
Controllers/ folder that should luckily be in the namespace folder, which is
All this hard work only to get that the
App\Controllers\AuthController class exists in the
app/Controllers/AuthController.php file. In order to have composer scanning your entire application and create direct 1-to-1 associations of classes and files, run the following command:
composer dumpautoload -o
Keep in mind that if you already ran php artisan optimize –force, you don’t have to run this one anymore. Since the optimize command already tells composer to create an optimized autoload.