CakePHP CDN Integration

This tutorial helps you with integrating KeyCDN into your CakePHP CDN installation. You could use either a Pull or Push Zone but in most cases a Pull Zone is sufficient. The following example will work for CakePHP version 2.4 or higher. For more details about the global configuration variables visit http://book.cakephp.org/3.0/en/development/configuration.html

  1. Create a pull zone before you start the CakePHP CDN integration.
  2. Define the base URL variables in your ./Config/bootstrap.php config file.
    ...
    Configure::write('App.imageBaseUrl', 'http://lorem-1.kxcdn.com/img/');    
    Configure::write('App.cssBaseUrl', 'http://lorem-1.kxcdn.com/css/');    
    Configure::write('App.jsBaseUrl', 'http://lorem-1.kxcdn.com/js/');
    ...

    Use your CDN URL or Zonealias plus the web path to the relevant public directory under webroot.

  3. Use the HTML helper to include your assets in your views or template.HtmlHelper::css
    <?php echo $this->Html->css('your-css'); ?>

    Will output:

    <link rel="stylesheet" type="text/css" href="http://lorem-1.kxcdn.com/css/your-css.css" />

    HtmlHelper::image

    <?php echo $this->Html->image('your-logo.png', array('alt' => 'Your Logo')); ?>

    Will output:

    <img src="http://lorem-1.kxcdn.com/img/your-logo.png" alt="Your Logo" />

    HtmlHelper::script

    <?php echo $this->Html->script('your-js'); ?>

    Will output:

    <script type="text/javascript" href="http://lorem-1.kxcdn.com/js/your-js.js"></script>
  4. Check in the HTML source code if your assets are loading from KeyCDN.