-->

Configure output dir for Assetic in Symfony2

2019-03-09 22:09发布

问题:

I'd like to globally configure the output dir of where assetic dumps my JS files. Currently, they always go to web/js/*. I want to change this to web/js/compiled/*.

It's possible to specify this at a per-file level: http://symfony.com/doc/2.0/cookbook/assetic/asset_management.html#dumping-asset-files

Can't seem to find a way to set this globally across my Symfony app. Any config parameter I'm missing?

UPDATE

Found an assetic config parameter called write_to. Setting this in config.yml causes the command line assetic:dump to dump files to the new dir, but within twig files the asset_url var still points to the original path.

回答1:

You should use the property write_to.

in my configuration for exemple I use

# Assetic Configuration
assetic:
    debug:          %kernel.debug%
    use_controller: %kernel.debug%
    read_from:      %kernel.root_dir%/Resources/views/
    write_to:       %kernel.root_dir%/../web/static/

Your ouput string start where ends write_to

for exemple

{% javascripts filter="closure" output='js/main.js'

...

 {% stylesheets filter='compass,?cssrewrite' 
     'default/static/sass/screen.scss' 
     output='css/screen.css' 
 %} 

both will placed respectively in /web/static/js/main.js and /web/static/css/screen.css

assets_base_urls is used to specify base URL's to be used for assets referenced from http and ssl (https) pages.

!! assets_base_urls is also used by {% images %} as the root before output value, but {% images %} doesn't consider write_to when rendering html (only when dumping) so better not using write_to and rely only on output value. More about it in my other post on stackoverflow and in this post on AsseticBundle's github.



回答2:

You can set the asset path ( assets_base_urls ) for twig to a static path, instead of using the relative path. In your config.yml file, it would look similar to this:

framework:
   templating:
      engines: ['twig']
         assets_base_urls:
            http: [http://path.to-cdn.com]

This will effect asset_url from assetic as well as twig's asset() method. The latter may or may not be desired.



回答3:

This GitHub issue comment helped me with this issue. While in dev, your assets will still go thru the controller but in production, the URLs will be as you desire.

Example config.yml:

assetic:
    write-to:  %kernel.root_dir%/../web/assets
    ...
framework:
    ...
    templating:
        engines: ['twig']
        packages:
            assetic:
                base_urls: '/assets'

Example in your template:

{% block javascripts %}
    {% javascripts '@jquery' '@bootstrap_js' '@backbone' '@handlebars' combine=true package='assetic' %}
    <script src="{{ asset_url }}"></script>
    {% endjavascripts %}
{% endblock %}

Notice that you have to add the package='assetic' attribute in the java scripts tag. This is a good compromise IMO because it won't break assets from other bundles as kmfk's solution will.



回答4:

Just a quick note on this. If you're using assets_base_urls, to specify a relative base URL, this only works prior to Symfony 2.7, due to the introduction of the new assets component in that version. Further information on how to change this is available at http://symfony.com/blog/new-in-symfony-2-7-the-new-asset-component , but the long and short of it is that:

framework:
    templating:
        assets_base_urls: 
            http: ['/some-relative-url']
            ssl: ['/some-relative-url']

becomes:

framework:
    assets:
        base_path: /some-relative-url


回答5:

Try this commande $ app/console --env=prod assetic:dump web/ you have juste to change the url you want raher than 'web/'