Support

Find answers, guides, and tutorials to supercharge your content delivery.

Yii2 CDN Integration

Updated on October 4, 2018
Yii2 CDN Integration

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 range 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 named cdn.

  3. If you are delivering a style.css file for instance, create another folder name 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 named 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:

    //...
    Yii::$app->cdn->get('style')->register();
    //...
    
  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:

List Zones GET

var_dump(Yii::$app->keycdn->get('zones.json'));

Purge Zone by URL DELETE

var_dump(Yii::$app->keycdn->delete(
    'zones/purgeurl/{zone_id}.json',
    [
        'urls' =>
        [
            'example-hexid.kxcdn.com/img/lorem.jpg'
        ]
    ]
));

Responses

Responses are returned as PHP's generic empty class stdClass. The following is an expected response when using Purge Zone URL.

object(stdClass)[194]
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.

Supercharge your content delivery 🚀

Try KeyCDN with a free 14 day trial, no credit card required.

Get started
KeyCDN uses cookies to make its website easier to use. Learn more