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.
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.
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.