Laravel provides a different way to clear the cache of the application. No matter whether it is configuration cache, view cache, route cache, etc. While working in the localhost, it will be easy to clear these all cache through the artisan command. You can clear all these caches separately through their individual command. Also, you can clear all cache using a single command. I am sure, you already know about clearing the cache in Laravel using the command line. But, it is a tedious job if you don’t have the command line/terminal access. For example, if you’ve deployed your application on cPanel but don’t have SSH access. Then how this job will be done there? Hence, in this post, I will show you the Laravel cache clear implementation without the command line.
Prerequisites
We will be implementing this functionality in Laravel 9. But, you can use the same in the previous version of Laravel projects. Hence, for creating a Laravel 9 project, you will need to have the following configurations.
- PHP >=8.0.2
- Composer
- Apache/Nginx Server
- VS Code Editor (Optional)
- MySQL (version > 5)
Once, you are ready, let’s start by creating a project.
Logout Multiple Auth Session From Other Devices in Laravel 9
Create Project For Laravel Cache Clear Functionality
I assume you already have a Laravel project setup. If not then you can create using the below command.
composer create-project --prefer-dist laravel/laravel myapp
Once the project setup is completed, let’s quickly configure the database.
Configure Database in Laravel
I already have a database for this, so, going to configure the same. Navigate to the .env file and add the below configuration details.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE={{DB_NAME}}
DB_USERNAME={{DB_USER_NAME}}
DB_PASSWORD={{DB_PASSWORD}}
After DB configuration, let’s move to the next step.
Laravel Mailgun Integration For Sending Email in Laravel 9
Laravel Cache Clear Implementation Using Artisan Call Function
We will implement the functionality at the route level. That means we will write a callback function in the route itself. You can create any controller and put the below functions in that controller as well.
Navigate to the web.php file and create the below routes with callback functions.
// Clear route cache
Route::get('/route-cache', function() {
$exitCode = Artisan::call('route:cache');
return 'Routes cache cleared successfully';
});
// Clear config cache
Route::get('/config-cache', function() {
$exitCode = Artisan::call('config:cache');
return 'Config cache cleared successfully';
});
// Clear application cache
Route::get('/clear-cache', function() {
$exitCode = Artisan::call('cache:clear');
return 'Application cache cleared successfully';
});
// Clear view cache
Route::get('/view-clear', function() {
$exitCode = Artisan::call('view:clear');
return 'View cache cleared successfully';
});
Now, you can run the application and try hitting these URLs in the browser. It will clear the cache.
But, hitting all these URLs one by one in the browser is a time taking work. So, why don’t we make something more easy way to get done this job?
Let’s implement another way to clear the entire application cache in a single trigger. So, add the below snippet.
// Clear all cache with a single function
Route::get('/optimize-clear', function() {
$exitCode = Artisan::call('optimize:clear');
return 'Application cache cleared successfully';
});
You can run access this route in the browser to check the result.
So, that’s it for this post, we implemented Laravel cache clear functionality. I hope you will find helpful to this post.
Leave a Reply