silverstripe/silverstripe-framework — pull request #3503 by nikrolls (2014-09-23 01:48:47+00:00)
Title: Alternate Assets Base URL feature

I propose a new feature on the `File` class that allows you to serve assets from a separate domain. It can be used for things such as:

* Serving files from a 'cookie-less' subdomain: set up an alternate vhost pointing to the existing assets folder and activate this option to update all asset links
* Serving files from a subdomain for use with CDNs that like to cache via CNAME: same implementation as above option
* Serving files from an optimised or centralised service: for example syncing assets to Amazon S3 and activating this option to point to the correct domain. Combined with a centralised or replicating database this enables load balancing between multiple servers.

**Configuration**
```php
# _ss_environment.php
define('SS_ALTERNATE_ASSETS_BASE_URL', '//files.example.com/')
```
```yml
# config.yml
---
Name: my-cdn
---
Only:
Environment: 'live'
---
File:
alternate_base_url: '//files.example.com/'
```

**Template**
```html
- Source template -
$SiteConfig.Logo <%-- A hypothetical Image field on SiteConfig --%>
Download our menu <%-- A hypothetical File field on the current page --%>
```
```html
- Alternate Assets Base URL disabled -

Download our menu
```
```html
- Alternate Assets Base URL enabled -

Download our menu
```

This pull request also includes tests for the new feature and a documentation update to ensure it's discoverable; Note that I'm unsure if it's possible to unit test the `_ss_environment.php` option without breaking other tests in the same run, as you cannot unset constants at runtime.

I'd be interested in any feedback on this. I understand that some discussion and larger strategy is going on regarding making SilverStripe more friendly when it comes to CDNs and load balancing, however this is a simple change that could provide a lot of functionality for people choosing to use it.