Everytime when I change something in templates I have to clear cache manually. Is there some way to disable templates caching in development mode?
- 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.Jason Lewis– Jason Lewis2013-06-07 00:59:02 +00:00Commented 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– wino2013-06-07 05:40:12 +00:00Commented Jun 7, 2013 at 5:40
- 14It's my fault. PhpStorm by default preserves files timestamp after upload.wino– wino2013-06-11 18:52:59 +00:00Commented 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.Dustin Graham– Dustin Graham2013-08-05 22:33:56 +00:00Commented Aug 5, 2013 at 22:33
- @wino Try this: stackoverflow.com/questions/20579182/…Volatil3– Volatil32014-01-14 14:24:43 +00:00Commented Jan 14, 2014 at 14:24
9 Answers
According to this request, change your application cache driver to array for the local environment.
Comments
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 Comments
... 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. :)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); }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 Comments
opcache.revalidate_freq=0and also set the server date/time to be behind my client as you suggested.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.
Comments
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); } } Comments
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.
Comments
Just put this somewhere in your app:
if (env('APP_DEBUG')) ini_set('opcache.revalidate_freq', '0'); 1 Comment
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