{"id":878,"date":"2015-11-28T23:04:02","date_gmt":"2015-11-29T04:04:02","guid":{"rendered":"http:\/\/xfloyd.net\/blog\/?p=878"},"modified":"2015-11-29T00:33:24","modified_gmt":"2015-11-29T05:33:24","slug":"878","status":"publish","type":"post","link":"https:\/\/xfloyd.net\/blog\/?p=878","title":{"rendered":"Loading assets from a different server"},"content":{"rendered":"<p>In a production environment you\u2019ll eventually want to load assets (images, CSS and JS) from a different server. Perhaps you\u2019re running your own asset server or you\u2019re even using a CDN. This will require the files to be linked with an absolute URL. But in your development environment you don\u2019t want this \u2013 you want assets to be loaded directly from the same server.<br \/>\n<!--more--><\/p>\n<p>So this is what you do. Use Symfony\u2019s asset() function for every asset you want to load from the asset server:<\/p>\n<pre>\r\n&lt;link rel=&quot;stylesheet&quot; href=&quot;{{ asset(&#39;css\/style.css&#39;) }}&quot; type=&quot;text\/css&quot; media=&quot;all&quot; \/&gt;\r\n&lt;script type=&quot;text\/javascript&quot; src=&quot;{{ asset(&#39;js\/scrips.js&#39;) }}&quot;&gt;&lt;\/script&gt;\r\n&lt;img src=&quot;{{ asset(&#39;images\/teaser.png&#39;) }}&quot; \/&gt;\r\n<\/pre>\n<p>If you\u2019re using Assetic you\u2019ll have to add the asset() function to asset_url:<\/p>\n<pre>{% javascripts &#39;@AcmeFooBundle\/Resources\/public\/js\/*&#39; %}\r\n    &lt;script type=&quot;text\/javascript&quot; src=&quot;{{ asset(asset_url) }}&quot;&gt;&lt;\/script&gt;\r\n{% endjavascripts %}<\/pre>\n<p>Now you can have different configuration for your production and development environment:<\/p>\n<pre>framework:\r\n  templating:\r\n    engines: ['twig']\r\n    assets_base_urls: http:\/\/assets.yourdomain.com\r\n<\/pre>\n<pre>framework:\r\n  templating:\r\n    assets_base_urls: ~ # Files are loaded with relative path\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In a production environment you\u2019ll eventually want to load assets (images, CSS and JS) from a different server. Perhaps you\u2019re running your own asset server or you\u2019re even using a CDN. This will require the files to be linked with an absolute URL. But in your development environment you don\u2019t want this \u2013 you want [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"_links":{"self":[{"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/878"}],"collection":[{"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=878"}],"version-history":[{"count":10,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/878\/revisions"}],"predecessor-version":[{"id":888,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/878\/revisions\/888"}],"wp:attachment":[{"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}