Assetic generating links but no files
Asked Answered
S

5

25

I'm trying to use assetic in symfony2 to manage my css. The links are generated fine. However, no files are generated.

Here's my configuration:

Layout.html.twig

    {% stylesheets
      '@FooBundle/Resources/public/css/main.css'
      filter='cssrewrite'
    %}
    <link rel="stylesheet" href="{{ asset_url }}" />
    {% endstylesheets %}

Config.yml

assetic:
debug:          %kernel.debug%
use_controller: false
bundles:        [ FooBundle ]
filters:
    cssrewrite: ~

Config_dev.yml

assetic:
use_controller: true

Assetic generates te link foo.foo/app_dev.php/css/957d3aa_main_1.css. However, the file isn't there (or anywhere else). I've tried playing around with permissions and looking in the (nginx) logs, but nothing so far.

All help would be greatly appreciated.

Suspension answered 2/6, 2012 at 15:14 Comment(0)
G
36

You have 2 options when dealing with assets. The reason you do not see your assets physically in your computer is because you chose Option 1.


Option 1: SYMFONY CAN PROCESS THE FILES DYNAMICALLY FOR YOU

That means that each asset path generated in the dev environment is handled dynamically by Symfony. Therefore, Assetic generates paths to CSS and JavaScript files that don't physically exist on your computer. This is an internal Symfony controller that opens the files and serves back the content for you.

Advantages: - Changes made on your assets take immediate effect - This is great in dev mode as Symfony generates the files dynamically for you

Disadvantages: - This is not possible in prod mode as rendering each asset dynamically would be too slow - The assets won't be directly accessible on your computer (which is why you cannot find the file) - Can be quite slow if you are using a lot of filters, etc...

To do this in dev mode, just edit assetic config in config_dev.yml:

assetic:
    use_controller: true

Option 2: DUMPING ASSET FILES

If you don't want to handle the assets dynamically, you can dump your assets manually, meaning actually writing your assets phisically on your computer.

Advantages: - No need for Symfony to generate the files dynamically so this will run a lot faster - Therefore, this is perfect in prod mode - The files are physically accessible in the web/ directory (or wherever you chose to output them)

Disadvantages: - You either need to dump the assets each time you change something..or you can dump the assets with the --watch command, which can potentially be a bit annoying if you are working in dev mode.

To do this:

Set use_controller to false (config_dev.yml):

assetic:
    debug:          %kernel.debug%
    use_controller: false

You can even choose where to read and output your assets if necessary

   assetic:
        read_from:      %kernel.root_dir%/Resources/views/
        write_to:       %kernel.root_dir%/../web/thefolderyouwant/

The ouput now starts from your write_to config in assetic

{% stylesheets
  '@FooBundle/Resources/public/css/main.css'
  output='css/main.css'
%}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}

You will have a resource error if you continue, so comment out or delete these assetic route in config_dev.yml:

_assetic:
    resource: .
    type:     assetic

Finally, you can generate the assets automatically, so that the changes that you make take immediate effect:

php app/console assetic:dump --watch

In that case, the file should now be available:

/web/thefolderyouwant/css/main.css

See the Cookbook for more info: How to use Assetic for Asset Management?

Garden answered 6/6, 2012 at 11:42 Comment(9)
I think it's better to edit your first answer instead of posting a new one.Zamora
This works! File is now created. However, /app_dev.php/css/2c29233.css gives a 404; without app_dev.php it doesn't. I've tried every possible nginx config, but can't get it to work :( Gonna try a bit more. If I can't figure it out I'll post another question and put up a link here. Thanks eitherway! (p.s. Can't award the bounty for another 17 hours.. I will, no worries)Suspension
I'm really sorry but it turns out it doesn't work. It only works when dumping the assets first. It should work without.. That was the point :SSuspension
You definitely need to dump the assets as you set up use_controller: false in your config. Symfony is no longer generating these assets for you. You can add --watch when dumping the assets to add them automatically.Garden
Also use_controller being on false, you don't go through the front controller app_dev.php. Therefore, Assetic does not add app_dev.php/ if you don't use it....which explains your 404 error.Garden
That's why I have use_controller set to true in config_dev.yml. Even when I set it to true in config.yml, the files aren't being generated...Suspension
See my EDIT, your files aren't generated because this is what you chose to do (Option 1). Hope this helps.Garden
Check the comment of June 7 15:51 -> Even when I set it to true, the files aren't being generated..Suspension
The link is also well created for me but the file is not served (nginx 404)Judithjuditha
E
0

I had the same problem, I just needed to install java

sudo apt-get install default-jre

you can also look on the begining of output, this might help:

app/console assetic:dump > outfile 2>&1
Edict answered 9/5, 2014 at 14:45 Comment(1)
This is for Java based filters (YUI compressor) only.Immolation
T
0

It also doesn't generate files when use_controller: true is on if you're using SASS to compile SCSS but ruby or the ruby gem sass not installed.

Trautman answered 23/12, 2014 at 21:45 Comment(0)
P
0

I had an error very similar to this one. Suddenly assetic stopped working. The only thing I added was the FOSRestBundle. Maybe you are using the rest bundle too.

Here is my solution:

fos_rest:
    routing_loader:
        default_format: json

    param_fetcher_listener: true
    body_listener: true

    format_listener:
        rules:
            # render "/api" requests as json
            - { path: ^/api, priorities: [ json ], fallback_format: json, prefer_extension: true }
            # default, fallback rendering twig templates
            - { path: ^/, priorities: ['html', 'application/javascript', 'text/css', '*/*'], fallback_format: html, prefer_extension: true }

I changed priorities: ['html', '*/*'] to priorities: ['html', 'application/javascript', 'text/css', '*/*'] and everything works fine now.

Pignut answered 1/8, 2016 at 12:34 Comment(0)
C
0

I have finally found solution !! I worked on this issue for hours! You wrote:

'@FooBundle/Resources/public/css/main.css' filter='cssrewrite'

BUT 'cssrewrite' filter doesn't accept @FooBundle syntax! You have to do:

php app/console assets:install

Symfony will create:

web/bundles/yourbundle/css/main.css

Now, in your twig template, replace:

'@FooBundle/Resources/public/css/main.css' filter='cssrewrite'

with:

'bundles/yourbundle/css/main.css' filter='cssrewrite'

Hope it's going to help someone else! (it's written in Symfony docs.. ^^)

Cycle answered 3/6, 2019 at 18:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.