Zend CDN integration in 2025

Why Use CDN in Zend Framework?

Zend Framework is an open-source, object-oriented web application framework built in PHP. It provides robust, reusable components for building modern web applications. By integrating a CDN (Content Delivery Network) with Zend Framework, you can significantly improve the speed, reliability, and scalability of your website. A CDN helps deliver static assets like images, CSS, and JavaScript faster by caching them across a global network of servers, reducing latency and load times for end users. Using a CDN with Zend Framework ensures efficient content delivery, optimized performance, and a better user experience, especially for sites with heavy traffic or users in various geographic locations.

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 Zend Framework 1

Define CDN in Bootstrap

Add the following init function to the Zend's application/Bootstrap.php file.

protected function _initCdn() 
{ 
    Zend_Registry::set('cdn_enabled', true);
    Zend_Registry::set('cdn_domain', 'cdn.mycompany.com');
    Zend_Registry::set('cdn_protocol', 'https');
}

Create CDN helper

class Zend_View_Helper_Cdn extends Zend_View_Helper_Abstract
{           
public 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: ' .
            'https://domain.com/image.jpeg.'
        );
    }

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

    if(!Zend_Registry::get('cdn_enabled'))
    {
        return $url;
    }    
    else
    {
        $uri = Zend_Registry::get('cdn_protocol') . '://' . Zend_Registry::get('cdn_domain') . $url;

        return $uri;
    }    
}    
}

Use the CDN helper in views

<?php echo $this->cdn('/image.jpeg'); ?>

will output

https://cdn.mycompany.com/image.jpeg    

Enable CDN in Zend Framework 2

Define CDN in Config

Add the following configuration to the module.config.php file.

return [
    'cdn' => [
        'enabled' => true,
        'domain'  => 'cdn.mycompany.com',
        'protocol' => 'https',
    ],
];

Create CDN helper

namespace Application\View\Helper;

use Zend\View\Helper\AbstractHelper;
use Zend\ServiceManager\ServiceLocatorInterface;

class CdnUrl extends AbstractHelper
{
    protected $cdnEnabled;
    protected $cdnDomain;
    protected $cdnProtocol;

    public function __construct($config)
    {
        $this->cdnEnabled = $config['enabled'];
        $this->cdnDomain = $config['domain'];
        $this->cdnProtocol = $config['protocol'];
    }

    public function __invoke($url)
    {
        $url = ltrim($url, '/');

        if (!$this->cdnEnabled) {
            return '/' . $url;
        }

        return $this->cdnProtocol . '://' . $this->cdnDomain . '/' . $url;
    }
}

Register the helper in module.config.php

'view_helpers' => [
    'factories' => [
        'cdnUrl' => function($container) {
            $config = $container->get('config')['cdn'];
            return new Application\View\Helper\CdnUrl($config);
        },
    ],
],

Use the CDN helper in views

<?php $this->cdnUrl('/image.jpeg'); ?>

will output

https://cdn.mycompany.com/image.jpeg    

Enable CDN in Zend Framework 3

Define CDN in Config

Add the following configuration to the module.config.php file.

return [
    'cdn' => [
        'enabled' => true,
        'domain'  => 'cdn.mycompany.com',
        'protocol' => 'https',
    ],
];

Create CDN helper

namespace Application\View\Helper;

use Laminas\View\Helper\AbstractHelper;

class CdnUrl extends AbstractHelper
{
    protected $cdnEnabled;
    protected $cdnDomain;
    protected $cdnProtocol;

    public function __construct(array $config)
    {
        $this->cdnEnabled = $config['enabled'] ?? false;
        $this->cdnDomain = $config['domain'] ?? '';
        $this->cdnProtocol = $config['protocol'] ?? 'https';
    }

    public function __invoke($url)
    {
        $url = ltrim($url, '/');

        if (!$this->cdnEnabled) {
            return '/' . $url;
        }

        return $this->cdnProtocol . '://' . $this->cdnDomain . '/' . $url;
    }
}

Register the helper in module.config.php

'view_helpers' => [
    'factories' => [
        Application\View\Helper\CdnUrl::class => function($container) {
            $config = $container->get('config')['cdn'];
            return new Application\View\Helper\CdnUrl($config);
        },
    ],
    'aliases' => [
        'cdnUrl' => Application\View\Helper\CdnUrl::class,
    ],
],

Use the CDN helper in views

<?php $this->cdnUrl('/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.

Contact Us

 _____      ______    _____   
|  __ \\   /_   _//  / ____|| 
| |  \ ||   -| ||-  / //---`' 
| |__/ ||   _| ||_  \ \\___   
|_____//   /_____//  \_____|| 
 -----`    `-----`    `----`