Using CDN when combining assets with Symfony2 and Assetic

I would like to use CDN along with Assetic in my Symfony2 project. I use the javascripts helper to combine multiple Javascript files:

 {% javascripts '@MyBundle/Resources/public/js/file-1.js' '@MyBundle/Resources/public/js/file-2.js' %} <script src="{{ asset_url }}"></script> {% endjavascripts %} 

and in my config.yml file I registered a CDN to be used in assets:

 framework: templating: assets_base_urls: http: [http://my.cdn.url] ssl: [https://my.cdn.url] 

When dumping, I get a combined file, but its URL is relative, not just one pointing to the CDN. For instance:

 <script src="/js/c713f83.js"></script> 

And this happens when combining multiple CSS files. The only way to get URLs using CDN is through asset :

 <img src="{{ asset('bundles/mybundle/images/logo.png') }} "> 

Is there anything that prevents Assetic from using the CDN hosts that I specified in my configuration?

+6
source share
1 answer

You must pass the asset() Twig function generated with Assetic asset_url :

 {% javascripts '@MyBundle/Resources/public/js/file-1.js' '@MyBundle/Resources/public/js/file-2.js' %} <script src="{{ asset(asset_url) }}"></script> {% endjavascripts %} 

Remember that in a dev environment, you will get URLs that look like http://my.cdn.url/app_dev.php/js/file-1.js . To prevent the dev environment from being configured to use the CDN:

 # app/config/config_dev.yml framework: templating: assets_base_urls: http: [] ssl: [] 

Remember that you dumped your assets using assetic:dump and, in general, remember that Assetic and Symfony2 asset The Twig function are two different things.

+9
source

Source: https://habr.com/ru/post/944450/


All Articles