Introduction
A CDN can continue to accelerate public images, stylesheets, JavaScript, and other static files used by legacy Zend Framework 1 applications. Dynamic application requests remain on the origin while cacheable files are delivered through the CDN Service Domain.
The original Zend Framework 1 is discontinued and no longer receives official Zend updates. This guide preserves its CDN integration pattern for applications that still use ZF1 or the zf1-future community continuation. For new applications or migrated projects, use the Laminas CDN integration guide instead.
Before you start
-
This tutorial shows how to integrate a CDN service into a website.
In this particular demo example we will use:
- barsoom-cdn.cdnsun.org as the CDN Service Domain, and
- https://barsoom.cdnsun.org as the website URL (i.e. barsoom.cdnsun.org is the Origin Domain).
- Please visit the Services/How-To section to obtain your CDN Service Domain.
- Before you take any steps please back up your files and database.
- 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 Service Domain is ready-to-use here.
Create CDN Static service
Please refer to Creating a CDN Static service for more details.
Configure CDN in Zend Framework 1
-
Open application/Bootstrap.php and register the complete HTTPS URL of the CDN Service Domain:
protected function _initCdn() { Zend_Registry::set( 'cdn_base_url', 'https://barsoom-cdn.cdnsun.org' ); } -
Create application/views/helpers/Cdn.php. The helper returns an origin-relative path when the CDN URL is empty and otherwise prefixes the asset path with the CDN Service Domain:
class Zend_View_Helper_Cdn extends Zend_View_Helper_Abstract { public function cdn($path) { $assetPath = '/' . ltrim((string) $path, '/'); $cdnBaseUrl = rtrim( (string) Zend_Registry::get('cdn_base_url'), '/' ); return $cdnBaseUrl === '' ? $assetPath : $cdnBaseUrl . $assetPath; } } -
Use the helper when rendering static asset URLs in Zend Framework 1 views:
<img src="<?php echo $this->cdn('/images/example.jpg'); ?>" alt="Example" > <link rel="stylesheet" href="<?php echo $this->cdn('/css/app.css'); ?>" > -
Open the public site and confirm that the frontend remains styled normally and static assets load from the CDN Service Domain.
Verify & Troubleshoot
- 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 Service Domain.
- Don't see your CDN Service 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.