使用Symfony2清除缓存命令时,静态资源不会刷新。

13

我正在使用Assetic与compass过滤器来传递和编译.scss文件。这一部分设置似乎运行良好。然而,我的理解是,在app_dev环境中,Symfony 2会为每个页面加载重新编译所有资产(包括css),而不使用其在prod环境中采用的缓存系统。

然而,这似乎并没有发生。

当我对一个.scss文件进行更改时,它只有在我使用以下命令时才会生效:

 app/console cache:clear

我原以为开发环境的目的就是避免每次这样做?!

我已经检查了缓存文件夹的权限(为了安全起见,我已将它们设置为任何人都可以读写)。有人有什么建议吗?


我使用Symfony和less,只要更改在实际包含的文件中而不是从该文件导入的文件中,它就会在每个请求上重新编译。也许这就是你的问题所在? - solarc
6个回答

15

如果你正在开发环境中使用Symfony 2 assets,只需使用这个命令

php app/console assets:install
php app/console assetic:dump --watch

自2.4版本开始,--watch已被废弃,并已被以下内容替代:

php app/console assetic:watch

详情请参见:http://symfony.com/blog/new-in-symfony-2-6-smarter-assets-install-command 和 http://symfony.com/doc/current/cookbook/assetic/asset_management.html - Dung

10

我想我在这里找到了答案:

assetic compass filter, css not updating when changing imported file(谷歌论坛讨论)

如果改变了导入的文件,但是没有更改父文件,则不会重新编译父文件。结果是,在强制重新编译之前,更改将不会被看到。

谷歌论坛上的帖子建议通过编辑AsseticController来实现可能的修复(hack!)。我还没有尝试过它,但即使它有效,我也不想编辑供应商包。


3

我知道这是一个老话题,但我能找到的唯一答案是CompassElephantBundle和上面的AsseticController hack。我的方法很简单,就是编辑复制原始的AsseticController,然后在参数中链接到它。

我所做的方式是编辑复制原始的AsseticController,然后在参数中链接到它,这样就不必编辑供应商包了。

parameters:
    assetic.controller.class: Acme\RandomBundle\Controller\AsseticController

复制的AsseticController只是从源路径中使用preg_match匹配文件类型,并从那里纠正缓存。
<?php

/* Original Assetic Controller */

public function render($name, $pos = null)
{
    if (!$this->enableProfiler && null !== $this->profiler) {
        $this->profiler->disable();
    }

    if (!$this->am->has($name)) {
        throw new NotFoundHttpException(sprintf('The "%s" asset could not be found.', $name));
    }

    $asset = $this->am->get($name);
    if (null !== $pos && !$asset = $this->findAssetLeaf($asset, $pos)) {
        throw new NotFoundHttpException(sprintf('The "%s" asset does not include a leaf at position %d.', $name, $pos));
    }

    $bustCache = preg_match('/\.(scss|sass|less)$/', $asset->getSourcePath());

    $response = $this->createResponse();
    $response->setExpires(new \DateTime());

    if ($bustCache) {
        $lastModified = time();
        $date = new \DateTime();
        $date->setTimestamp($lastModified);
        $response->setLastModified($date);
    }
    else
    {
        // last-modified
        if (null !== $lastModified = $asset->getLastModified()) {
            $date = new \DateTime();
            $date->setTimestamp($lastModified);
            $response->setLastModified($date);
        }
    }

    // etag
    if ($this->am->hasFormula($name)) {
        $formula = $this->am->getFormula($name);
        $formula['last_modified'] = $lastModified;
        $response->setETag(md5(serialize($formula)));
    }

    if ($response->isNotModified($this->request)) {
        return $response;
    }

    if ($bustCache) {
        $response->setContent($asset->dump());
    }
    else {
        $response->setContent($this->cachifyAsset($asset)->dump());
    }

    return $response;
}

/* Rest of controller */

3

资源编译不属于缓存系统的一部分。无论在哪个环境下,当您进行更改时,都需要重新安装资源。

app/console assets:install web

如果您所使用的文件系统支持符号链接,您可以避免为每个更改运行此命令,而只需将资源安装为这样。
app/console assets:install web --symlink

但是,由于您正在使用Sass,这可能不是一个选项。
希望对您有所帮助。

抱歉,Peter,我应该表达得更清楚。我只是在谈论资产文件(css、js),而不是像图片这样的资产。在文档中(http://symfony.com/doc/current/cookbook/assetic/asset_management.html)指出,在开发环境中,这些资产是动态处理的,更改应立即显示。 - musoNic80

2

我已经通过在parameters.yml的末尾添加一行代码来解决了本地开发中的此问题,该代码基本上停止了任何资产缓存。

# parameters.yml
...

assetic.cache.class: Assetic\Cache\ArrayCache

这段代码绝不应该在生产环境中使用,因为我们希望进行缓存!


0

我使用不同的方法。在开发过程中,我会添加所有的.scss文件。

    {% block stylesheets %}
        {% stylesheets filter='?uglifycss' filter='cssrewrite' filter="compass"
            "@TESTSSassBundle/Resources/public/css/_vars.scss" <-- this will be removed
            "@TESTSSassBundle/Resources/public/css/main.scss"
            "@TESTSSassBundle/Resources/public/css/header.scss"
            "@TESTSSassBundle/Resources/public/css/footer.scss"
         %}
            <link rel="stylesheet" href="{{ asset_url }}" />
        {% endstylesheets %}
    {% endblock %}  

在我完成开发后,我会将它们删除。 这样我就不需要清除缓存或添加/更改任何设置。这对我总是有效的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接