Codementor Events

Laravel: Clearing Cache with a click of a button (Artisan Shortcuts)

Published Feb 01, 2018
Laravel: Clearing Cache with a click of a button (Artisan Shortcuts)

Laravel: Cache https://blog.42mate.com*

In this tutorial we will learn how to purge compiled views and cache by using Laravel Commands.

Switching back and forth between applications might be cumbersome during development for some folks who have a single monitor. Thanks to the commands in Laravel, we can create and call those commands from anywhere.

Before you begin, keep in mind that you should only use this method for Development purposes. Because purging and re-caching might effect your site's performance

Let's start by creating a controller for our operations like this:

php artisan make:controller DevClearCacheController

Now add following methods to DevClearCacheController class like this:

<?php

namespace App\Controllers;

class DevClearCacheController extends Controller
{
    public function clear_views()
    {
        \Artisan::call('view:clear');
        
        return redirect()->back()->with('status','Views Cleared!');
    }
    
    public function clear_cache()
    {
        \Artisan::call('cache:clear');
        
        return redirect()->back()->with('status','Cache Cleared!');
    }
    // you can also add methods for queue:start, queue:restart etc.
}

Now let's add these to the web.php file in the routes directory.

<?php
...

Route::get('clear-views', '[email protected]_views')
    ->name('clear-views');
Route::get('clear-cache', '[email protected]_cache')
    ->name('clear-cache');
    
...

Now, let's create a view that contains our buttons (I prefer to add these in navbar):

<!DOCTYPE html>
<html lang="{{ App::getLocale() }}"><head>
<meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>{{ config('app.name', 'Artisan Shortcuts') }}</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css">
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
            <ul class="nav navbar-nav">
            <li><a href="{{ route('clear-views') }}">Clear Views</a></li> 
            <li><a href="{{ route('clear-cache') }}">Clear Cache</a></li> 
            </ul>
        </div><!--/.nav-collapse -->
    </div>
</nav>
<div class="body-content">
  @if (session('status'))
    <div class="alert alert-success">{{ session('status') }}</div>
    @endif
    <p>Test App</p>
</div>
</body>
</html>

Now let's test our application, the result will be similar to the following:

Laravel Views Cache Clearing with buttonLaravel Views Cache Clearing
Laravel Cache Clearing with buttonLaravel Cache Clearing

Discover and read more posts from Tahir Jan
get started
post commentsBe the first to share your opinion
Show more replies