27

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

6
  • Are you referring to Blade? You shouldn't have to clear the compiled views as if you change a view Blade will re-compile it. Jun 7, 2013 at 0:59
  • Yes. I'm using blade. I don't see changes when I updating someting in templates. I have to remove cache files from app/store/views directory manually.
    – wino
    Jun 7, 2013 at 5:40
  • 14
    It's my fault. PhpStorm by default preserves files timestamp after upload.
    – wino
    Jun 11, 2013 at 18:52
  • I (think I) had a similar need to disable the view caching, in the case where I was working on a blade extension. Altering the Blade::extend code didn't cause the view cache to refresh. Aug 5, 2013 at 22:33
  • @wino Try this: stackoverflow.com/questions/20579182/…
    – Volatil3
    Jan 14, 2014 at 14:24

9 Answers 9

12

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.

7

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

6

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.

3
  • 2
    Change cache driver to array for the local environment.
    – Emeka Mbah
    Apr 15, 2015 at 9:11
  • I had to remove ... implements Middleware. By the way, when I set the cache driver: CACHE_DRIVER=array, view cache files continued to be written to: framework/views/<cache files>. I would still prefer a solution that doesn't affect every request. This works for the time being. Thank you. :) May 19, 2015 at 16:46
  • I added to local dev .env the following: ENABLE_MIDDLEWARE_KILL_VIEWS_CACHE=true And then in the middleware: public function handle($request, Closure $next) { // added this block if (!env('ENABLE_MIDDLEWARE_KILL_VIEWS_CACHE')) { return $next($request); } May 19, 2015 at 16:59
4

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.

2
  • This worked, thanks! For me, I was using phpStorm (to sync the files using sftp) and I had to both set the opcache.revalidate_freq=0and also set the server date/time to be behind my client as you suggested. Mar 9, 2015 at 16:11
  • In fact for using FTP access between two OS the date and time makes difference when renewing the cache. As access to the virtual machine by PhpStorm was generating this problem, I changed the date of the VM and it worked. Nov 11, 2016 at 12:32
4

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.

2

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);
    }
}
1

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.

1

Just put this somewhere in your app:

if (env('APP_DEBUG')) ini_set('opcache.revalidate_freq', '0');
1
  • As @edvinas.me said, you need PHP 5.5+ to use it. For more information, see This Question Apr 13, 2015 at 19:48
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.