Laravel is an open-source PHP framework designed to build robust web applications using the Model-View-Controller (MVC) architecture. During development, you might encounter situations where your changes don’t reflect immediately, often caused by cached data.
This guide explains how to efficiently clear various types of caches in Laravel, ensuring a smooth development experience. We’ll cover two primary methods: using Artisan commands and through the web browser.
Why Clear Cache in Laravel?
Laravel caches data like routes, configurations, views, and application-level data to enhance performance. However, during development, this caching can sometimes lead to outdated content being displayed. Clearing the cache resolves these issues, allowing you to see the latest changes.
Clearing Cache in Laravel Using Artisan Commands
Artisan, Laravel’s command-line tool, provides an easy way to clear different caches. Follow these steps:
Open the Terminal: Navigate to your Laravel application’s root directory.
Execute the Relevant Commands: Use the following Artisan commands to clear specific caches:
Clear Views Cache:
php artisan view:clear
This command clears cached views in your Laravel application.
Clear Route Cache:
php artisan route:clear
This command removes cached route data.
Clear Application Cache:
php artisan cache:clear
This clears the application cache.
Clear Configuration Cache:
php artisan config:clear
Use this to clear configuration-related cache files.
Clearing Cache in Laravel Through a Web Browser
If you cannot access the terminal, you can clear the cache via your web browser by defining specific routes in your Laravel application.
Steps to Clear Cache via Browser
Add Routes for Cache Clearing: Update your web.php
routes file with the following code:
// Clear application cache: Route::get('/clear-cache', function() { Artisan::call('cache:clear'); return 'Application cache has been cleared'; }); // Clear route cache: Route::get('/route-cache', function() { Artisan::call('route:clear'); return 'Routes cache has been cleared'; }); // Clear config cache: Route::get('/config-cache', function() { Artisan::call('config:clear'); return 'Config cache has been cleared'; }); // Clear view cache: Route::get('/view-clear', function() { Artisan::call('view:clear'); return 'View cache has been cleared'; });
Access the Routes via Browser:
Open your web browser and navigate to the corresponding URL (e.g., http://yourdomain.com/clear-cache
).
Each route clears a specific type of cache and displays a confirmation message.
Conclusion
Clearing caches in Laravel is a straightforward process that ensures your application runs smoothly during development. Whether you use Artisan commands or browser routes, keeping your cache updated is crucial for maintaining application integrity.
For more tips and insights, explore the Host.co.in Knowledge Base. Ready to build your Laravel application? Check out our Laravel-optimised hosting plans for unmatched performance.