Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class S3_Controller extends Extension {
- public function onBeforeInit() {
- $this->postRequest();
- }
- public function postRequest(SS_HTTPRequest $request, DataModel $model = null) {
- $response = $this->owner->response;
- if ($body = $response->getBody()) {
- $body = self::replaceCDN($body);
- $response->setBody($body);
- }
- return $response;
- }
- static function replaceCDN($body) {
- if (!$body) {
- return;
- }
- $cdn = "//" . S3_BUCKET . ".s3.amazonaws.com/assets"; // or a working cname record
- $myDomain = Director::absoluteBaseURL();
- echo $myDomain;
- die;
- $body = str_replace('src="assets/', 'src="'.$cdn.'/', $body);
- $body = str_replace('href="assets/', 'href="'.$cdn.'/', $body);
- $body = str_replace($myDomain . 'assets/', $cdn.'/', $body); //absolute links
- return $body;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement