Laravel/Blade缓存CSS文件 [英] Laravel/blade caching css files

查看:419
本文介绍了Laravel/Blade缓存CSS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on Nginx server, with PHP-FPM. I installed Laravel 4.1 and bootstrap v3.1.1., and here is the problem. For the last 30 minutes, I have been trying to change a css rule that I first declared to check boostrap.

.jumbotron{
   background: red; 
}

第一次使用.超大容器是红色的.因此,我删除了该css值并开始工作,但是无论我使用哪个浏览器,该容器都是红色的.我什至通过Google Chrome浏览器检查工具检查了css文件,它向我显示了jumbotron具有background:red时的第一个值.我删除了css文件,并将其重命名并添加了新样式,我将chrome配置为不缓存页面.但是还是一样的价值.我现在确信,Laravel保留了第一个样式声明的缓存.

The first time it worked. The jumbotron container was red. So, I removed that css value and started working, but still no matter which browse I use, the container is red. I even checked the css file through the Google Chromes inspection tool, and it is showing me that first value when jumbotron had a background:red. I deleted the css file and renamed it and add new styles, I configured chrome not to cache pages. But Still the same value. I'm convinced now, that Laravel has kept a cache of the first style declaration.

有没有办法完全禁用此功能?

Is there any way to disable this at all?

推荐答案

一般说明

访问Laravel Blade视图时,它将生成一个临时文件,因此不必每次访问视图时都需要处理Blade语法.这些文件以文件名(文件路径的MD5哈希)存储在app/storage/view中.

通常,当您更改视图时,Laravel会在下次访问视图时自动重新生成这些文件,然后一切正常.通过比较 filemtime() 功能.在您的情况下,可能是有问题,并且没有重新生成临时文件.在这种情况下,您必须删除这些文件,以便可以重新生成它们.它没有任何害处,因为它们是从您的视图自动生成的,并且可以随时重新生成.它们仅用于缓存目的.

Usually when you change a view, Laravel regenerate these files automatically at the next view access and everything goes on. This is done by comparing the modification times of the generated file and the view's source file through the filemtime() function. Probably in your case there was a problem and the temporary file wasn't regenerated. In this case, you have to delete these files, so they can be regenerated. It doesn't harm anything, because they are autogenerated from your views and can be regenerated anytime. They are only for cache purposes.

通常,它们应该自动刷新,但是如果这些文件被卡住并且遇到类似此类的问题,则可以随时删除这些文件,但是正如我所说的,这些只是罕见的例外.

Normally, they should be refreshed automatically, but you can delete these files anytime if they get stuck and you have problems like these, but as I said these should be just rare exceptions.

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

All the following codes are from laravel/framerok/src/Illuminate/View/. I added some extra comments to the originals.

Engines/CompilerEngine.php开始,我们掌握了理解力学的主要代码.

Starting from Engines/CompilerEngine.php we have the main code we need to understand the mechanics.

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中完成.这是一项重要功能.根据结果​​,将决定是否应重新编译视图.如果返回的是false而不是true,则可能是无法重新生成视图的原因.

Check if regeneration required

This will be done in Compilers/Compiler.php. This is an important function. Depending on the result it will be decided whether the view should be recompiled. If this returns false instead of true that can be a reason for views not being regenerated.

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

重新生成视图

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

Regenerate view

If the view is expired it will be regenerated. In Compilers\BladeCompiler.php we see that the compiler will loop through all Blade keywords and finally give back a string that contains the compiled PHP code. Then it will check if the view storage path is set and save the file there with a filename that is the MD5 hash of the view's filename.

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中评估视图.它使用 extract() trycatch中具有传递路径的文件,所有具有handleViewException()的异常都将再次引发该异常.也有一些输出缓冲.

Evaluate

Finally in Engines/PhpEngine.php the view is evaluated. It imports the data passed to the view with extract() and include the file with the passed path in a try and catch all exceptions with handleViewException() that throws the exception again. There are some output buffering too.

这篇关于Laravel/Blade缓存CSS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆