使用 assetic 时找不到包含在 CSS 中的 Symfony 文件

Symfony files which are included in CSS are not found when using assetic

我将 Symfony 与 Twig 一起使用,并且我使用 assetic。问题是我的 CSS 文件包含其他 CSS 文件,例如:

@import url(ie8.css);
@import url(blocks.css);
@import url(plugins.css);
@import url(app.css);

并且显示网页时找不到这些文件。 字体和图像也是如此。我该如何解决?

我不得不提一下,我加载 css 个文件是这样的:

        {% stylesheets
        '@BaseBundle/Resources/public/plugins/bootstrap/css/bootstrap.min.css'
        '@BaseBundle/Resources/public/css/style.css'
        filter='cssrewrite'
        %}
        <link rel="stylesheet" href="{{ asset_url }}" />
        {% endstylesheets %}

来自Symfony docs and this section as well

Notice that in the original example that included JavaScript files, you referred to the files using a path like @AppBundle/Resources/public/file.js, but that in this example, you referred to the CSS files using their actual, publicly-accessible path: bundles/app/css. You can use either, except that there is a known issue that causes the cssrewrite filter to fail when using the @AppBundle syntax for CSS stylesheets.

When using the cssrewrite filter, don't refer to your CSS files using the @AppBundle syntax. See the note in the above section for details.

cssrewrite 过滤器至关重要,因为它将重写您在样式表中定义的那些 url() CSS 路径。通过使用 @AppBundle 语法,这种情况不再发生。您需要像这样加载 CSS 个文件:

{% stylesheets
    'bundles/base/css/bootstrap.min.css'
    'bundles/base/css/style.css'
    filter='cssrewrite'
%}
    <link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}

注意路径可能不是 100% 正确的(这取决于您的包名称)。您应该通过执行 php app/console assets:install 仔细检查并查看您的 web/ 文件夹。