Laravel, a popular open-source PHP framework, simplifies web application development using the MVC (Model-View-Controller) architectural pattern.
During Laravel application development, you might encounter scenarios where code changes are not immediately reflected. This is often due to caching mechanisms. This guide will demonstrate how to effectively clear various types of caches within your Laravel applications.
There are primarily two methods for clearing caches in Laravel: using Artisan commands and triggering cache clearing through a web browser.
This article will cover the steps required to clear different cache types including configuration, view, route, and application caches, ensuring your Laravel application always reflects the latest updates.
Pick A Laravel Hosting Plans To Scale Laravel Applications!
Clearing Cache Using Artisan Commands
To begin, open your terminal, navigate to your Laravel application’s root directory, and then utilize the following Artisan commands to manage your project’s caches.
Clear Application Cache
To clear the main application cache, use the following command:
php artisan cache:clear
Clear Route Cache
To clear the cached routes of the application, execute the following command:
php artisan route:clear
Clear Configuration Cache
To remove the cached application configuration, run the artisan command as shown:
php artisan config:clear
Clear Compiled Views Cache
In order to remove the cached view files, use the artisan command provided below:
php artisan view:clear
Clearing Cache via Web Browser
You can also clear caches programmatically via your web browser, which can be especially helpful when you don’t have direct terminal access to your Laravel application’s server. To do that, you’ll first need to define specific routes within your Laravel application, as shown below:
// Clear application cache:
Route::get(‘clear-cache’, function() {
Artisan::call(‘cache:clear’);
});
———————————————————
//Clear route cache:
Route::get(‘route-cache’, function() {
Artisan::call(‘route:cache’);
return ‘Routes cache has been cleared;’;
});
———————————————————
//Clear config cache:
Route::get(‘config-cache’, function() {
Artisan::call(‘config:cache’);
return ‘Config cache has been cleared;’;
});
———————————————————
// Clear view cache:
Route::get(‘view-clear’, function() {
Artisan::call(‘view:clear’);
return ‘View cache has been cleared;’;
});
By following these steps, you can effectively clear both your application’s cache using Artisan commands, or via browser using pre-defined routes.