When you're working on a project, it's not unusual for a developer to make regularly changes to your layouts and views. The problem I've found with Laravel is it creates a cache of both views and layouts merged as one PHP file, although great for production sites, not so great in development. When you make a change to a view or layout, upload it to your server you may find that you don't see your changes, forcing you to log into FTP and clear the cache from the folder /storage/framework/views. Although this solution works, it's both time consuming and annoying.

There is a way to have these cache files cleared automatically upon each request. This can be done by creating a middleware which checks your current environment and determines if the files should be deleted. In my case I'm checking to see if the ENV property 'APP_ENV' is equal to 'staging'.

So, lets create some middleware /app/Http/Middleware/ClearLavaralCache.php folder;

namespace App\Http\Middleware\Admin; 
use Closure; 
use Illuminate\Contracts\Foundation\Application; 
* ClearLaravelCache  *  
* @param request The request object.  
* @param $next The next closure.  
* @return redirects to the secure counterpart of the requested uri.  
class ClearLaravelCache {  
    protected $app; 
    public function __construct(Application $app)  {
        $this->app = $app;

    public function handle($request, Closure $next)  {
        if (env('APP_ENV') == 'staging') {
            $cachedViewsDirectory = app('path.storage') . '/framework/views/';
            $files = glob($cachedViewsDirectory.'*'); 
            foreach($files as $file) {
                if(is_file($file)) {  

        return $next($request); 

Next end the file /app/Http/Kernel.php and add the following so that your new middleware code is executed upon every request;

protected $middleware = [

That should be it, your view cache should now be cleared upon each request;


There are no comments, why not be the first?

Submit a reply

Your e-mail address will not be published, all fields are required.


Get useful tips on web application development and software engineering.