laravel 如何在开发模式下禁用模板缓存?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16971445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How I can disable templates caching in development mode?
提问by wino
Everytime when I change something in templates I have to clear cache manually. Is there some way to disable templates caching in development mode?
每次在模板中更改某些内容时,我都必须手动清除缓存。有没有办法在开发模式下禁用模板缓存?
回答by phoops
If you are using PHP5.5, then I would suggest configuring opcache in php.ini
如果您使用的是 PHP5.5,那么我建议在 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.
此值设置应从缓存更新视图的时间频率。该值通常为 60 秒左右。将其设置为 0 将使您的缓存每次都更新。
回答by Simon Schneider
I used the solution of "Gadoma" some times. But since there is not "filters.php" in Laravel 5anymore, here is my middleware class for the newest Laravel version:
我曾多次使用“Gadoma”的解决方案。但是由于Laravel 5 中不再有“ filters.php” ,这里是我最新 Laravel 版本的中间件类:
<?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:
在你的 Kernel.php 中:
protected $middleware = [
...
'App\Http\Middleware\CacheKiller',
];
Not the nicest solution but it works.
不是最好的解决方案,但它有效。
回答by Vikas Khunteta
回答by Ifnot
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).
1 - 如果您通过 FTP 或其他远程协议工作,则两个操作系统上的日期可能不匹配。尝试将您的客户端放在将来或服务器放在过去(几秒钟就足够了)。
Reminder: for linux based os, a simple date --set
works, for example date --set 18:30:00
for 18h30 pm.
提醒:对于基于 linux 的操作系统,一个简单的date --set
工作,例如date --set 18:30:00
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 - (转发wino
评论)您的客户可能不会更新您编辑过的文件的时间戳。您必须编辑 IDE 的配置。
回答by Neo Ighodaro
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.
您可能会添加一个进程(取决于您的操作系统)来侦听文件更改并自动运行命令。
回答by Taylor
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.
在 laravel 5.2 中:创建一个新的中间件,$middlewareGroups
在Kernel.php
. 这将调用 artisan 命令来清除所有已编译的视图文件。
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);
}
}
回答by Gadoma
You can try this route filter, setting the cache time
to 0, that way your view will be recreated on every request :)
您可以尝试使用此路由过滤器,将 设置cache time
为 0,这样您的视图将在每次请求时重新创建 :)
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.
希望这可以帮助你,但我没有测试它,所以我不能保证它会起作用。
回答by Tim Ogilvy
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
从 PHP 5.3 到 PHP 5.5 升级的一些额外缓存问题可在此处获得:Laravel 和开发中的视图缓存——无法立即看到更改
回答by Rafael Beckel
Just put this somewhere in your app:
只需将其放在您的应用程序中的某个位置:
if (env('APP_DEBUG')) ini_set('opcache.revalidate_freq', '0');