Yii2 CDN Integration

yii2 cdn

Yii is an open source PHP framework used for developing web 2.0 applications. It allows you to quickly build applications by following 3 steps:

  • You create the database
  • Yii generates the base PHP code
  • You customize the code

The framework also comes with a wide ranges of features including MVC, design pattern, layered caching scheme, role based access control, testing, etc. In this integration article, we cover the steps required to complete a Yii2 CDN integration.

How to Complete a Yii2 CDN Integration

Using Yii in combination with KeyCDN is quite simple. Follow the steps below to complete a Yii2 CDN integration.

  1. Install the required extension through composer by running php composer.phar require --prefer-dist blacksmoke26/yii2cdn "*" or add "blacksmoke26/yii2cdn": "*" to the “require” section of your composer.json file.
  2. Under your root folder, create a directory called “cdn”.
  3. If you are delivering a style.css file for instance, create another folder called “style” and place your CSS assets within that folder. The path for this should then be /cdn/style.
  4. Open your web.php (@app/config/web.php) file and add property called “cdn” under the “components” section with the following code:
    // ...
    'components' => [
        // ...
        'cdn' => [
            'class' => '\yii2cdn\Cdn',
            'baseUrl' => '/cdn',
            'basePath' => dirname(dirname(__DIR__)) . '/cdn',
            'components' => [
                'style' => [
                    'css' => [
                            'css/style.css', // offline
                            '@cdn' => 'https://cdn.yourwebsite.com/css/style.css', // online version
      // ...
    // ...
  5. In a view file, paste the following:
  6. Check your HTML source to ensure that the asset URL has be rewritten to use the CDN URL instead of the origin.

Using a Yii Extension to Purge Your KeyCDN Zone

Apart from integrating a CDN into your Yii-based application, you can also use the Yii KeyCDN extension to purge your KeyCDN Zone. To install the extension, you can use the following composer command:

composer require Sammaye/yii2-keycdn "*@dev"

To use the extension it must be defined within the same @app/config/web.php file as mentioned above, under “components”. For example:

'keycdn' => [
    'class' => 'example\keycdn\KeyCdn',
    'apiKey' => 'your_api_key'

Usage Examples

Now that the extension is defined, it can be used within your project. A couple of usage examples include:

First Example – (GET) List Zones


Second Example – (DELETE) Purge zone by URL 

        'urls' => 


Responses are returned as PHP’s generic empty class stdClass. The following is an expected response when using purgeurl.

  public 'status' => string 'success' (length=7)
  public 'description' => string 'Cache has been cleared for URL(s).' (length=34)

If the response cannot be decoded from JSON or there is a status error, then the extension will throw a yii\base\Exception.

Benefits of using a Yii CDN Setup

Use the above method to start delivering your static content via a CDN to help improve performance and reduce latency of your Yii app. Although performance enhancements are a major benefit to using a CDN, there also exists many additional advantages to users who accelerate their Yii framework application with KeyCDN, such as:

For a full list of features that KeyCDN offers, check out our features page.