We moved this page to our Documentation Portal. You can find the latest updates here. |
To integrate CodeIgniter PHP framework with CDN, kindly follow the steps below.
- To make the process seamlessly, we recommend to load URL Helper globally by setting it on application/config/autoload.php and include 'url' in the loader.
$autoload['helper'] = array('url');
- Moving next, on config file at application/config/config.php, create the CDN URL variable and in this example, we will be using cdn.onappcdn.com. Please replace with your CDN URL:
$config['base_url'] = 'https://www.example.com/';
$config['cdn_url'] = 'https://cdn.onappcdn.com/';
- Thirdly, you would need to extend the URL Helper by updating this following code:
$config['subclass_prefix'] = 'MY_';
and replacing with ext.
$config['subclass_prefix'] = 'ext.';
- Last but no least, create a new helper file on application/helpers/ext.url_helper.php by extending the base_url functions. It's rewrites all the specifies static contents to CDN URL:
<?php
function base_url($uri) { $currentInstance =& get_instance(); $cdnUrl = $currentInstance->config->item('cdn_url'); $extensions = array('css', 'js', 'webp', 'jpg', 'jpeg', 'png', 'gif','pdf'); $pathParts = pathinfo($uri); if (!empty($cdnUrl) && in_array($pathParts['extension'],$extensions)) { return $cdnUrl . $uri; } return $currentInstance->config->base_url($uri); }
>
You may also add in any other prefer static filetypes that are required on the code above.