0

I'm trying to append CDN url to all image resources across the site.

current code looks like this $this->Html->image('img.png'). I found that there's an option to append baseURL ['fullBase' => true], but I couldn't find an option to append custom URL that would route to desired CDN. Is there a way to create a custom setting that would point to CDN url such as ['CDN_URL_1' => true] or any other clean way to achieve this?

The other approach I can think about is appending path like so $this->Html->image($customURL.'img.png'), but this might not be the correct approach.

Thanks!

1 Answer 1

2

You can define the base URL for those items in your config file (./Config/bootstrap.php).

See Configuration.

App.imageBaseUrl

Web path to the public images directory under webroot. If you are using a CDN you should set this value to the CDN’s location.

App.cssBaseUrl

Web path to the public css directory under webroot. If you are using a CDN you should set this value to the CDN’s location.

App.jsBaseUrl

Web path to the public js directory under webroot. If you are using a CDN you should set this value to the CDN’s location.

...
Configure::write('App.imageBaseUrl', 'http://yourcdn.com/img/');
Configure::write('App.jsBaseUrl', 'http://yourcdn.com/js/');
Configure::write('App.cssBaseUrl', 'http://yourcdn.com/css/');
...

Then just use $this->Html->image('img.png') normally.

Sign up to request clarification or add additional context in comments.

1 Comment

this is much cleaner compared to what I had in mind, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.