2012-07-03 38 views
4

我在一個現有的項目中使用Symfony2和資產,我無法弄清楚如何引用我的CSS中的圖像。我已經把我的圖片:src/Organisation/MyBundle/Resources/public/images/Symfony2 CSS圖像路徑得到重寫,我得到一個404

運行資產後安裝:php app/console assets:install public

我可以加載它們正確的瀏覽器:http://sitename/bundles/organisationmy/images/logo.png

然而,當我嘗試指向它們在CSS與:url('/bundles/organisationmy/images/logo.png')

與螢火蟲檢查,在URL中獲取的改變: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隨着更改資產自動更新。

回答

8

爲了加載在你的CSS文件中的圖像的路徑必須是"bundles/(bundle name)/images/(file name)"

UPDATE

請確保您有

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 
+0

是,戴維,謝謝你。正如我所提到的,這可以從瀏覽器中完美地工作,我可以從瀏覽器加載圖像。正如解釋的那樣,問題出在我將網址放入CSS中。 – stivlo

+0

我的設置和你一樣,在我的CSS文件中:background:url('/ bundles/mscore/img/prev.png')left 48%no-repeat; ,我也有write_to:%kernel.root_dir%/ ../web下asset:在config.yml –

+0

所以它應該工作,你有沒有看到問題底部的**更新**?我認爲問題可能是我的CSS加載不再更新。 – stivlo