CodeIgniter CDN Integration
CodeIgniter is an open source PHP framework released in 2006 by EllisLab and is now developed and maintained by British Columbia Institute of Technology. It is intended for developers wanting a simple and elegant way to create powerful applications.
This integration requires the use of the URL Helper and that your application uses the base_url
function to return the designated content, for example:
<img src="<?php echo base_url('assets/img/logo.svg'); ?>" alt="logo">
<!-- <img src="https://www.example.com/assets/img/logo.svg" alt="logo"> -->
<link rel="stylesheet" href="<?php echo base_url('assets/css/style.css'); ?>">
<!-- <link rel="stylesheet" href="https://www.example.com/assets/css/style.css"> -->
<script type="text/javascript" src="<?php echo base_url('assets/js/app.js'); ?>"></script>
<!-- <script type="text/javascript" src="https://www.example.com/assets/js/app.js"></script> -->
How to complete a CodeIgniter CDN integration
This integration guide demonstrates how to complete a CodeIgniter CDN integration with KeyCDN. Follow the steps below to successfully integrate your CodeIgniter application with KeyCDN:
Create a Pull Zone for the origin server.
Load URL Helper globally. Open the
application/config/autoload.php
and look for this item:$autoload['helper'] = array();
Add
url
to the array:$autoload['helper'] = array('url');
Note: Loading the URL Helper globally is optional. Alternatively load this helper where needed instead.Open the
application/config/config.php
file:Add the
$config['keycdn_url']
item. To set the value add the followingKeyCDN URL
snippet below theBase Site URL
snippet:/* |-------------------------------------------------------------------------- | Base Site URL |-------------------------------------------------------------------------- | | URL to your CodeIgniter root. Typically this will be your base URL, | WITH a trailing slash: | | http://example.com/ | | WARNING: You MUST set this value! | | If it is not set, then CodeIgniter will try guess the protocol and path | your installation, but due to security concerns the hostname will be set | to $_SERVER['SERVER_ADDR'] if available, or localhost otherwise. | The auto-detection mechanism exists only for convenience during | development and MUST NOT be used in production! | | If you need to allow multiple domains, remember that this file is still | a PHP script and you can easily do that on your own. | */ $config['base_url'] = 'https://www.example.com/'; /* |-------------------------------------------------------------------------- | KeyCDN URL |-------------------------------------------------------------------------- | | URL to your KeyCDN Zone. Use your Zone URL or Zone Alias, | WITH a trailing slash: | | http://examplepull-hexid.kxcdn.com/ | http://cdn.example.com/ | | WARNING: You MUST set this value to enable KeyCDN! | | If it is not set, then the extended URL Helper will use your default | Base Site URL instead. | */ $config['keycdn_url'] = 'https://examplepull-hexid.kxcdn.com/';
Extend the URL Helper that assists with working with URLs. To set your own prefix look for this item:
$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 theapplication/helpers/ext.url_helper.php
file:<?php /* | Base URL | | Overwrite the base_url function to support | loading designated content from KeyCDN. */ function base_url($uri) { $currentInstance =& get_instance(); $keycdnUrl = $currentInstance->config->item('keycdn_url'); // define any extension that should use your KeyCDN URL $extensions = array('css', 'js', 'svg', 'jpg', 'jpeg', 'png', 'gif', 'pdf'); $pathParts = pathinfo($uri); if (!empty($keycdnUrl) && in_array($pathParts['extension'], $extensions)) { return $keycdnUrl . $uri; } return $currentInstance->config->base_url($uri); }
Verify the designated content is loading from KeyCDN. To check this inspect the HTML source code of your application, for example:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Welcome to CodeIgniter</title> <link rel="stylesheet" href="https://examplepull-hexid.kxcdn.com/assets/css/style.css"> </head> <body> <div id="container"> <h1>Welcome to CodeIgniter!</h1> <div id="body"> <img src="https://examplepull-hexid.kxcdn.com/assets/img/logo.svg" alt="logo"> <p>The page you are looking at is being generated dynamically by CodeIgniter.</p> </div> </div> <script type="text/javascript" src="https://examplepull-hexid.kxcdn.com/assets/js/app.js"></script> </body> </html>
Note: You can use a custom subdomain by creating a Zone Alias.
Upon completing this CodeIgniter CDN integration, your CodeIgniter application will deliver designated content from our global network. This will significantly improve the performance and user experience of your CodeIgniter application. Adding a CDN to your CodeIgniter application will bring your content as close as possible to your users, decreasing the overall latency of your application.
Do you know of another integration approach with CodeIgniter that we should mention here? Contact us to let us know and we will add it.