wino
wino

Reputation: 271

How I can disable templates caching in development mode?

Everytime when I change something in templates I have to clear cache manually. Is there some way to disable templates caching in development mode?

Upvotes: 27

Views: 35788

Answers (9)

Taylor
Taylor

Reputation: 443

In laravel 5.2: Create a new middleware, add to 'web' $middlewareGroups in Kernel.php. This will call the artisan command to clear all compiled view files.

namespace App\Http\Middleware;

use Artisan;
use Closure;

class ClearViewCache
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (env('APP_ENV') === 'local') {
            Artisan::call('view:clear');
        }

        return $next($request);
    }
}

Upvotes: 2

Vikas Khunteta
Vikas Khunteta

Reputation: 1484

According to this request, change your application cache driver to array for the local environment.

Upvotes: 7

Simon Schneider
Simon Schneider

Reputation: 1236

I used the solution of "Gadoma" some times. But since there is not "filters.php" in Laravel 5 anymore, here is my middleware class for the newest Laravel version:

<?php
namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Routing\Middleware;

class CacheKiller implements Middleware {

    /**
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        $cachedViewsDirectory = app('path.storage').'/framework/views/';

        if ($handle = opendir($cachedViewsDirectory)) {

            while (false !== ($entry = readdir($handle))) {
                if(strstr($entry, '.')) continue;    
                @unlink($cachedViewsDirectory . $entry);    
            }

            closedir($handle);
        }

        return $next($request);

    }

}

And in your Kernel.php:

protected $middleware = [
        ...
        'App\Http\Middleware\CacheKiller',
    ];

Not the nicest solution but it works.

Upvotes: 6

Rafael Beckel
Rafael Beckel

Reputation: 2504

Just put this somewhere in your app:

if (env('APP_DEBUG')) ini_set('opcache.revalidate_freq', '0');

Upvotes: 1

Neo
Neo

Reputation: 886

It's harder to debug when I don't really understand your configuration. All I can offer as help is instead of deleting the view cache directly you can run:

$ php artisan cache:clear

You could probably add a process (depending on your OS) to listen for file changes and automatically run the command.

Upvotes: 4

ek9
ek9

Reputation: 3442

If you are using PHP5.5, then I would suggest configuring opcache in php.ini

opcache.revalidate_freq=0

This value sets the time frequency when views should be updated from cache. This value is usually 60 seconds or so. Setting it to 0 will make your cache update every time.

Upvotes: 12

Ifnot
Ifnot

Reputation: 5103

It looks that blade are using file timestamp for rebuilding pages.

So if pages are not directly updated by blade there are several options :

1 - If you are working by FTP or other remote protocol you may have the date mismatching on the two OS. Try to put your client in the future or the server in the past (few seconds is enough).

Reminder : for linux based os, a simple date --set works, for example date --set 18:30:00 for 18h30 pm.

2 - (Repost wino comment) You client may does not update the timestamp of your edited file. You have to edit the configuration of your IDE.

Upvotes: 4

Tim Ogilvy
Tim Ogilvy

Reputation: 1973

Some additional caching issues from a PHP 5.3 to PHP 5.5 upgrade avaliable here: Laravel and view caching in development -- can't see changes right away

Upvotes: 0

Gadoma
Gadoma

Reputation: 6565

You can try this route filter, setting the cache time to 0, that way your view will be recreated on every request :)

From this gist,

Route::filter('cache', function( $response = null )
{

    $uri = URI::full() == '/' ? 'home' : Str::slug( URI::full() );

    $cached_filename = "response-$uri";

    if ( is_null($response) )
    {
        return Cache::get( $cached_filename );
    }
    else if ( $response->status == 200 )
    {
        $cache_time = 30; // 30 minutes

        if ( $cache_time > 0 ) {
            Cache::put( $cached_filename , $response , $cache_time );
        }
    }

});

Hope this helps you out, but I didn't test it so I cant guarrantee it'll work.

Upvotes: 1

Related Questions