我在每个项目中都使用它:
class HTML extends Kohana_HTML
{
/**
* Given a file, i.e. /css/base.css, replaces it with a string containing the
* file's mtime, i.e. /css/base.1221534296.css.
*
* @param $file The file to be loaded. Must be an absolute path (i.e.
* starting with slash).
*/
public static function auto_version($file)
{
if (!file_exists($_SERVER['DOCUMENT_ROOT'] . '/' . $file))
return $file;
$mtime = filemtime($_SERVER['DOCUMENT_ROOT'] . '/' . $file);
return preg_replace('{\\.([^./]+)$}', ".$mtime.\$1", $file);
}
}
在我的模板视图中:
<link type="text/css" href="<?php echo HTML::auto_version('/media/css/global.css') ?>" rel="stylesheet" />
学分来自如何强制浏览器重新加载缓存的 CSS/JS 文件的 Kip 答案?
PS。请记住将以下规则添加到您的 .htaccess 中:
RewriteRule ^(.*)\.[\d]{10}\.(css|js)$ $1.$2 [L]