Why Use a CDN in CodeIgniter?
CodeIgniter is a lightweight PHP framework designed for speed and flexibility. However, as your website or application grows, delivering static assets such as images, stylesheets, and scripts directly from your server can slow things down, especially for users far from your hosting location.
A Content Delivery Network (CDN) helps solve this by distributing your static content across multiple servers worldwide. When a user visits your site, assets are loaded from the nearest CDN edge server, reducing latency and improving page load speed. Faster load times mean better user experience, lower bounce rates, and improved SEO rankings. Additionally, using a CDN lightens the load on your origin server, allowing it to focus on processing dynamic requests more efficiently.
Before you start
- Before you take any steps please back up your files and database.
- In the following, we will integrate a CDN service using the CDN domain cdn.mycompany.com Please visit the Services/How-To section to obtain your CDN domain.
- To integrate a CDN service on https:// website the CDN service must have SSL enabled.
- If your website embeds custom fonts then please first enable CORS for them.
- Before you take any steps please make sure that your CDN domain is ready-to-use here.
Create CDN Static service
Please refer to Creating a CDN Static service for more details.
Enable CDN in CodeIgniter
There are two methods to integrate a CDN in CodeIgniter: Using the built-in URL Helper and Creating a Custom URL Helper.
Method I - Using the built-in URL Helper
This method assumes you are already using the built-in base_url helper in your HTML code. For example:
<img src="<?php echo base_url('assets/img/logo.svg'); ?>" alt="logo">
Open the global configuration file application/config/config.php and add the cdnsun_url option:
$config['cdnsun_url'] = 'https://cdn.mycompany.com/';
Extend the built-in URL Helper that assists with working with URLs. To set your own prefix look for this option:
$config['subclass_prefix'] = 'MY_';
Rename the prefix to ext. instead:
$config['subclass_prefix'] = 'ext.';
Extend the existing base_url function. To overwrite this function create the application/helpers/ext.url_helper.php file:
<?php function base_url($uri) { $currentInstance =& get_instance(); $cdnsunUrl = $currentInstance->config->item('cdnsun_url'); // define any extension that should use your CDN URL $extensions = array('css', 'js', 'svg', 'jpg', 'jpeg', 'png', 'gif', 'pdf'); $pathParts = pathinfo($uri); if (!empty($cdnsunUrl) && in_array($pathParts['extension'], $extensions)) { return $cdnsunUrl . $uri; } return $currentInstance->config->base_url($uri); }
That's all. Verify that the content is loading from the CDN by inspecting your application's HTML source code.
Method II - Creating a Custom URL Helper
Define CDN in config
Add the following to the CodeIgniter's applicaiton/config/config.php file.
$config['cdn_enabled'] = true; $config['cdn_domain'] = 'cdn.mycompany.com'; $config['cdn_protocol'] = 'https';
Create CDN helper
Create a file application/helpers/cdn_helper.php with the following content.
<?php function cdn($url = null) { $url = (string) $url; if(empty($url)) { throw new Exception('URL missing'); } $pattern = '|^http[s]{0,1}://|i'; if(preg_match($pattern, $url)) { throw new Exception('Invalid URL. ' . 'Use: /image.jpeg instead of full URI: ' . 'http://domain.com/image.jpeg.' ); } $pattern = '|^/|'; if(!preg_match($pattern, $url)) { $url = '/' . $url; } $currentInstance =& get_instance(); $cdn_enabled = $currentInstance->config->item('cdn_enabled'); $cdn_domain = $currentInstance->config->item('cdn_domain'); $cdn_protocol = $currentInstance->config->item('cdn_protocol'); if(empty($cdn_enabled)) { return $url; } else { return $cdn_protocol . '://' . $cdn_domain . $url; } }
Update autoloader
Add the following to the CodeIgniter's application/config/autoload.php file.
$autoload['helper'] = array('cdn');
Use the CDN helper in views
<?php echo cdn('/image.jpeg'); ?>
will output
https://cdn.mycompany.com/image.jpeg
Notes
- View HTML source code of your web pages to verify that you are using CDN, you should see source attribute of your images, CSS, JavaScript, etc. beginning with your CDN domain.
- Don't see your CDN domain in the source code of your web pages? If your website is using any cache plug-in/mechanism then you might want to clear/flush its cache.
- Having trouble with custom fonts? Please refer to Using custom fonts with CDN - setting CORS for more details.
- Still having trouble? Check your CDN URLs using our content check tool or please refer to Debugging a CDN service for more hints.
- Can you improve this guide or write a new one for software we haven't covered? Help us expand our CDN integrations section and earn $50 in CDN credit! Contact us to contribute.