Search code examples
symfonyassetic

Assetic generating links but no files


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.


Solution

  • 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?