PHP Zend Framework CDN Integration
27 Oct 2013

PHP Zend Framework CDN Integration

Introduction

I’m going to describe how you can integrate CDN into PHP Zend Framework 1.11. As usually in Zend Framework, you can do it in many ways. In this post I’m going to show you one of the possible ways. I’m going to create and then integrate two CDN services, one for HTTP requests and the second one for HTTPS requests.

Requirements

I require that you know Zend Framework a little bit. Namely that you know how to:

  • create and use a plugin,
  • create and use a view helper,
  • use Zend_Registry.

Create a CDN service

    1. Log in or Sign up to CDNsun client section.
    2. On the Services tab choose New Service.
    3. Fill in the domain of your website and choose a domain for your new CDN service as in the picture below.create_cdn

Create a CDN SSL service

    1. Log in or Sign up to CDNsun client section.
    2. On the Services tab choose New Service.
    3. Click “Advanced Settings” and select “Shared SSL: Enabled”.
    4. Note that in this case your “Service Domain” has to end with “.r.worldssl.net” and it can be only a 4-th level domain.
    5. Fill in the domain of your website and choose a domain for your new CDN service as in the picture below.Create CDN SSL

Create CDN plugin in Zend Framework

class Application_Plugin_Cdn extends Zend_Controller_Plugin_Abstract 
{
public function preDispatch(Zend_Controller_Request_Abstract $request) 
{

$enable_for_development = (bool) false;
$enable_for_http = (bool) true;
$enable_for_https = (bool) true;
$cdn_hostname_for_http = 'static.myproject.com';
$cdn_hostname_for_https = 'myproject.r.worldssl.net';

if((APPLICATION_ENV == 'production') || (!empty($enable_for_development)))
{
    // HTTPS
    if($request->isSecure())
    {
        if(!empty($enable_for_https))
        {
            Zend_Registry::set('cdn_on', true);
            Zend_Registry::set('cdn_protocol', 'https');
            Zend_Registry::set('cdn_hostname', $cdn_hostname_for_https);
        }
        else
        {
           Zend_Registry::set('cdn_on', false);       
        }
    }
    else // HTTP
    {
        if(!empty($enable_for_http))
        {
            Zend_Registry::set('cdn_on', true);
            Zend_Registry::set('cdn_protocol', 'http');
            Zend_Registry::set('cdn_hostname', $cdn_hostname_for_http);
        }
        else
        {
           Zend_Registry::set('cdn_on', false);       
        }
    }
}
else
{
    Zend_Registry::set('cdn_on', false);            
}

return true;

}
}

Create CDN view helper in Zend Framework

class Zend_View_Helper_Cdn extends Zend_View_Helper_Abstract
{           
public function cdn($url)
{
    if(empty($url))
        throw new Exception('Url missing');

    $pattern = '/^http/i';        
    if(preg_match($pattern, $url))
    {
        throw new Exception('Bad usage. ' .
            'Use: /htdocs/images instead of ' .
            'http://myproject.com/htdocs/images.'
                           );
    }

    $pattern = '|^/|';        
    if(!preg_match($pattern, $url))
            $url = '/' . $url;

    if(!Zend_Registry::get('cdn_on'))
        return $url;
    else
    {
        $cdn_hostname = Zend_Registry::get('cdn_hostname');
        $cdn_protocol = Zend_Registry::get('cdn_protocol');
        $uri = $cdn_protocol . '://' . $cdn_hostname . $url;

        return $uri;
    }    
}    
}

Use the CDN view helper in your views

Use the CDN view helper in your view like this:

<img 
  alt="My Image" 
  src="<? echo $this->cdn('/htdocs/images/my_image.jpeg'); ?>"
/>

That’s all

Sincerely, CDNsun.