This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

📣 Try the fastest hosting platform with pay-as-you-go pricing & 24/7 expert support! MIGRATE NOW →

Integrate Cloudways CDN with CodeIgniter Projects

Updated on December 8, 2021

3 Min Read

Content Distribution Network or CDN has become an important aspect of website design. Given the global nature of the audience, a CDN is invaluable in ensuring fast page load speed for all visitors, regardless of the location of the visitor.

cdn in codeigniter

A CDN stores a copy of the static elements of the website on a series of servers called nodes. When a user requests a web page, the static elements are served from the nearest node of the CDN. This reduces the page load time significantly.

The good news is that you could easily integrate CDN within your CodeIgniter projects built on any PHP Hosting. This tutorial will guide you through the process of installing CloudwaysCDN on a Codeigniter project hosted on Cloudways.

The first step is the launch of a server and a PHP application on Cloudways. For more details on this, please check out the following GIF:

Login to your server and go to the Applications tab. Click the PHP application. Once the new screen opens, the tab for configuring CDN at the end of the options in the left-side panel.

Once you click the link, you will be redirected to the CDN screen where you could get the newly generated URL which will be used in the code application.

Now you need to extend the existing cdn_base_url function by overwriting it.

For this, start by creating the file: application/helpers/cdn_helper.php

function cdn_base_url($uri)

{

   $currentInstance =& get_instance();



   $keybasedcdnUrl = $currentInstance->config->item('cdn_based_key_url');



   $extensions = array('css', 'js', 'jpg', 'jpeg', 'png', 'gif','pdf');

   $pathParts = pathinfo($uri);



   if (!empty($keybasedcdnUrl) && in_array($pathParts['extension'],$extensions)) {

       return $keybasedcdnUrl . $uri;

   }



   return $currentInstance->config->cdn_base_url($uri);

}

This code will overwrite the cdn_base_url function to support and load the website assets from loading your asset from the key based CDN.

Get Ready for Core Web Vitals Update

Ebook to Speed Up Your Website Before You Start Losing Traffic.

Thank You

Your list is on it’s Way to Your Inbox.

The next step is setting up the CDN URL. For this, you need to modify this file: applicaiton/config/config.php. Add the folloing line into the file . Remember to replace the cdn based url with the proper URL:

$config['cdn_based_key_url']   = 'cdn based url';

Output

Here is the output in action:

Conclusion

In this tutorial, I discussed how you could integrate CDN with your CodeIgniter project. If you need help with the process, do leave a comment and I will get back to you.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Inshal Ali

Inshal is a Content Marketer at Cloudways. With background in computer science, skill of content and a whole lot of creativity, he helps business reach the sky and go beyond through content that speaks the language of their customers. Apart from work, you will see him mostly in some online games or on a football field.

×

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour

CYBER WEEK SAVINGS

  • 0

    Days

  • 0

    Hours

  • 0

    Mints

  • 0

    Sec

GET OFFER

For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now