Phalcon CDN Integration
Updated on January 6, 2023
This documentation guides you through the integration of KeyCDN with the high performance PHP framework Phalcon. There are different approaches to integrate KeyCDN, either use the base URI option, the asset management or simply a view helper which can produce all CDN URLs for your assets.
Create a Pull Zone before you start the Phalcon CDN integration.
The simplest but most generic approach is to define the
setStaticBaseUri
:<?php $url = new Phalcon\Mvc\Url(); // Dynamic URIs remain on your origin server $url->setBaseUri('/'); // Static resources go through KeyCDN $url->setStaticBaseUri('http://cdn.foobar.com/');
Alternative: Use collections and add a URL-prefix for your CDN URL.
<?php $css = $this->assets->collection('header'); $scripts = $this->assets->collection('footer'); if ($config->environment == 'development') { $css->setPrefix('/'); $scripts->setPrefix('/'); } else { $cdnURL = 'http://example-hexid.kxcdn.com/'; $css->setPrefix($cdnURL); $scripts->setPrefix($cdnURL); } $css->addCss('css/bootstrap.min.css') ->addCss('js/custom.css'); $scripts->addJs('js/jquery.js') ->addJs('js/bootstrap.min.js');
Alternative: In case you don't want to use collections, use this approach:
... $cdnURL = 'http://example-hexid.kxcdn.com/'; $this->assets ->addCss($cdnURL.'css/custom.css', false); ...
As always, verify the HTML source code if the assets are pointing to your CDN URL.