Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 CSS images path gets rewritten and I get a 404

I'm using Symfony2 and assetic in an existing project and I can't figure out how to refer to images in my CSS. I've put my images in: src/Organisation/MyBundle/Resources/public/images/

After running the assets install: php app/console assets:install public

I can load them in the browser correctly: http://sitename/bundles/organisationmy/images/logo.png

However, when I try to point to them in the CSS, with: url('/bundles/organisationmy/images/logo.png')

inspecting with firebug, the url gets changed in: url('/web/bundles/organisationmy/images/logo.png') and that results in a 404.

In my main template I have:

{% 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'
 %}

And in config.yml I have:

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

In routing.yml I have:

_assetic:
    resource: .
    type: assetic

I've seen this question Symfony2 - Assetic - load images in CSS, but I don't know if and how I can use filter='cssrewrite' and filter='yui_css' at the same time, or how to solve the problem.

Basically if I could stop Symfony2 to prefix my CSS images paths with /web/ the problem would be solved. Or, does anyone know what is the proper way to solve this problem in Symfony2?

UPDATE: I've noticed that even if I put url('something.png') the CSS doesn't get updated anymore when I load the page, so I might have messed up something with command line, I've also given commands like php app/console assetic:dump -- how to go back to the normal dev style of modify CSS and reload page?

UPDATE2: I discovered that php app/console assetic:dump --watch updates the assets automatically as they change.

like image 673
stivlo Avatar asked Jul 03 '12 14:07

stivlo


1 Answers

In order to load images in your CSS file the path must be "bundles/(bundle name)/images/(file name)"

UPDATE

make sure you have

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

under assetic: in config.yml

and

background: url('/bundles/(bundle name)/images/prev.png') in your css

then run

php app/console assets:install web 

and

php app/console assetic:dump web
like image 178
Dave Mascia Avatar answered Nov 15 '22 04:11

Dave Mascia