css - Laravel/blade 缓存 css 文件

标签 css twitter-bootstrap caching laravel laravel-4

我正在使用 PHP-FPM 开发 Nginx 服务器。我安装了 Laravel 4.1bootstrap v3.1.1.,这就是问题所在。在过去的 30 分钟里,我一直在尝试更改我首先声明的用于检查 boostrap 的 css 规则。

.jumbotron{
   background: red; 
}

第一次成功。超大屏幕容器是红色的。所以,我删除了那个 css 值并开始工作,但无论我使用哪种浏览器,容器仍然是红色的。我什至通过 Google Chromes 检查工具检查了 css 文件,当 jumbotron 有一个 background:red 时,它向我显示了第一个值。我删除了 css 文件并将其重命名并添加了新样式,我将 chrome 配置为不缓存页面。但仍然是相同的值(value)。我现在确信,Laravel 保留了第一个样式声明的缓存。

有什么方法可以完全禁用它吗?

最佳答案

一般解释

当您访问 Laravel Blade View 时,它将生成一个临时文件,因此它不必在您每次访问 View 时都处理 Blade 语法。这些文件存储在 app/storage/view 中,文件名是文件路径的 MD5 哈希值。

通常,当您更改 View 时,Laravel 会在下一次访问 View 时自动重新生成这些文件,一切都会继续进行。这是通过 filemtime() 比较生成文件和 View 源文件的修改时间来完成的。功能。在您的情况下,可能存在问题并且未重新生成临时文件。在这种情况下,您必须删除这些文件,以便重新生成它们。它不会造成任何伤害,因为它们是根据您的 View 自动生成的,并且可以随时重新生成。它们仅用于缓存目的。

通常情况下,它们应该会自动刷新,但如果它们卡住并且您遇到此类问题,您可以随时删除这些文件,但正如我所说,这些应该只是极少数异常(exception)。

代码分解

以下所有代码均来自laravel/framerok/src/Illuminate/View/。我在原件上添加了一些额外的评论。

获取 View

Engines/CompilerEngine.php 开始,我们有了理解机制所需的主要代码。

public function get($path, array $data = array())
{
    // Push the path to the stack of the last compiled templates.
    $this->lastCompiled[] = $path;

    // If this given view has expired, which means it has simply been edited since
    // it was last compiled, we will re-compile the views so we can evaluate a
    // fresh copy of the view. We'll pass the compiler the path of the view.
    if ($this->compiler->isExpired($path))
    {
        $this->compiler->compile($path);
    }

    // Return the MD5 hash of the path concatenated
    // to the app's view storage folder path.
    $compiled = $this->compiler->getCompiledPath($path);

    // Once we have the path to the compiled file, we will evaluate the paths with
    // typical PHP just like any other templates. We also keep a stack of views
    // which have been rendered for right exception messages to be generated.
    $results = $this->evaluatePath($compiled, $data);

    // Remove last compiled path.
    array_pop($this->lastCompiled);

    return $results;
}

检查是否需要再生

这将在 Compilers/Compiler.php 中完成。这是一个重要的功能。根据结果​​,将决定是否应重新编译 View 。如果这返回 false 而不是 true,这可能是 View 未重新生成的原因。

public function isExpired($path)
{
    $compiled = $this->getCompiledPath($path);

    // If the compiled file doesn't exist we will indicate that the view is expired
    // so that it can be re-compiled. Else, we will verify the last modification
    // of the views is less than the modification times of the compiled views.
    if ( ! $this->cachePath || ! $this->files->exists($compiled))
    {
        return true;
    }

    $lastModified = $this->files->lastModified($path);

    return $lastModified >= $this->files->lastModified($compiled);
}

重新生成 View

如果 View 过期,它将被重新生成。在 Compilers\BladeCompiler.php 中,我们看到编译器将循环遍历所有 Blade 关键字,最后返回一个包含已编译 PHP 代码的字符串。然后它将检查是否设置了 View 存储路径,并将文件保存在那里,文件名是 View 文件名的 MD5 哈希值。

public function compile($path)
{
    $contents = $this->compileString($this->files->get($path));

    if ( ! is_null($this->cachePath))
    {
        $this->files->put($this->getCompiledPath($path), $contents);
    }
}

评估

最后在 Engines/PhpEngine.php 中评估 View 。它使用 extract() 导入传递给 View 的数据并且 includetry 中包含传递路径的文件,并 catch 使用 handleViewException() 抛出的所有异常再次异常(exception)。还有一些输出缓冲。

关于css - Laravel/blade 缓存 css 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23411463/

相关文章:

docker - 使用 Docker Buildkit 构建镜像时 --cache-from 参数的顺序是否重要?

html - 如何为溢出隐藏的div提供填充底部?

css - Scala.js - 将 css 文件的内容注入(inject)内联 <style> 标签

macos - Mac : There was an error while executing `VBoxManage` , Vagrant 使用的 CLI

javascript - 如果函数被多次调用,在函数中缓存选择器是否性能更高?

javascript - 在平板电脑上禁用 Bootstrap 模式的淡入淡出

Jquery 根据通配符 ID 添加 .css 到标签

css - vue 路由器链接中的中心元素

css - 从控件/输入中删除 Bootstrap 样式

CSS Bootstrap 覆盖了我自己的 CSS