【问题标题】:Symfony2 CSS images path gets rewritten and I get a 404Symfony2 CSS 图像路径被重写,我得到 404
【发布时间】:2012-07-03 23:39:55
【问题描述】:

我在现有项目中使用 Symfony2 和资产,但我不知道如何在我的 CSS 中引用图像。我已将图片放入:src/Organisation/MyBundle/Resources/public/images/

运行资产安装后:php app/console assets:install public

我可以在浏览器中正确加载它们:http://sitename/bundles/organisationmy/images/logo.png

但是,当我尝试在 CSS 中指向它们时,使用:url('/bundles/organisationmy/images/logo.png')

使用萤火虫检查,网址更改为:url('/web/bundles/organisationmy/images/logo.png'),导致 404。

在我的主模板中:

{% stylesheets filter='yui_css' output='css/output.css'
    '@MyBundle/Resources/public/css/bootstrap.min.css'
    '@MyBundle/Resources/public/css/bootstrap-responsive.min.css'
    '@MyBundle/Resources/public/css/my.css'
 %}

config.yml 我有:

assetic:
    debug: %kernel.debug%
    use_controller: false
    java: /usr/bin/java
    filters:
        cssrewrite: ~
        yui_css:
            jar: %kernel.root_dir%/java/yuicompressor-2.4.7.jar
        yui_js:
            jar: %kernel.root_dir%/java/yuicompressor-2.4.7.jar

routing.yml 我有:

_assetic:
    resource: .
    type: assetic

我已经看到这个问题Symfony2 - Assetic - load images in CSS,但我不知道我是否可以以及如何同时使用filter='cssrewrite'filter='yui_css',或者如何解决问题。

基本上,如果我可以阻止 Symfony2 为我的 CSS 图像路径添加 /web/ 前缀,问题就会得到解决。或者,有谁知道在 Symfony2 中解决这个问题的正确方法是什么?

更新:我注意到即使我输入 url('something.png') 加载页面时 CSS 也不会再更新,所以我可能搞砸了通过命令行,我还给出了类似php app/console assetic:dump 的命令——如何回到正常的开发风格修改 CSS 并重新加载页面?

UPDATE2:我发现php app/console assetic:dump --watch 会在资产更改时自动更新它们。

【问题讨论】:

    标签: css image symfony yui-compressor


    【解决方案1】:

    为了在您的 CSS 文件中加载图像,路径必须是 "bundles/(bundle name)/images/(file name)"

    更新

    确保你有

    write_to: %kernel.root_dir%/../web 
    

    assetic:config.yml

    background: url('/bundles/(bundle name)/images/prev.png') 在你的 CSS 中

    然后运行

    php app/console assets:install web 
    

    php app/console assetic:dump web
    

    【讨论】:

    • 好的,戴夫,谢谢。正如我所提到的,这可以在浏览器中完美运行,我可以从浏览器加载图像。问题是当我将 URL 放入 CSS 中时,正如解释的那样。
    • 我的设置和你一样,在我的 CSS 文件中我有: background: url('/bundles/mscore/img/prev.png') left 48% no-repeat; ,我也有 write_to: %kernel.root_dir%/../web 在assetic: in config.yml
    • 所以它应该可以工作,你看到问题底部的 update 了吗?我认为问题可能是我加载的 CSS 不再更新。
    • 确保你有 write_to: %kernel.root_dir%/../web 在assetic: in config.yml 和 background: url('/bundles/mscore/img/prev.png') in你的 CSS 然后运行 ​​php app/console assets:install web 然后 php app/console assets:dump web
    • 这很好用!谢谢戴夫,现在我也可以从 CSS 背景中看到图像了。但是,虽然每次添加图像时都可以运行 php app/console assets:install web,但如果每次修改 CSS 时都必须运行 php app/console assetic:dump web(需要 1 分钟),它会减慢我的速度。它曾经在我的开发机器上工作,我修改了 CSS,并且立即看到更改,你知道为什么不再工作了吗?
    猜你喜欢
    • 2014-05-01
    • 2011-05-27
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-28
    • 2019-08-27
    • 1970-01-01
    相关资源
    最近更新 更多