CDN PHP

Laravel 4 – CDN Token Authentication Helper

For those of you using Laravel 4 and the Webair CDN with Token Authentication, I’ve written a handy little helper so that you can insert the tokenized CDN URL right into your templates. This will also work with other CDN providers that have Token Authentication.

In app/start/global.php add app/libraries to the paths to load classes from:

ClassLoader::addDirectories(array(
    app_path().'/commands',
    app_path().'/controllers',
    app_path().'/models',
    app_path().'/database/seeds',
    //my custom paths (THIS)
    app_path().'/libraries',
));

 

Now go to the app/libraries directory and create your class in MyCdnHelper.php:

<?php
//define the class
class MyCdnHelper {

    //define your CDN hostname
    protected static $url = "http://cdn.example.com";

    //define your token auth key
    protected static $key = "primary token auth key here";

    //define your Time to Live value in seconds (ex. 86400 = 24 hours)
    protected static $ttl = 86400;

/**
 * cdnEncode
 * This method will use your token auth key to create a unique time sensitive hash for your CDN links to prevent Hotlinking
 *
 * @param $request
 *
 * @return string
 *
 * @access public
 * @static
 */
    public static function cdnEncode($request) {

        $uri = "/" . $request;
        $now = time();
        $expiration = $now + self::$ttl;

        $uri = str_replace('//','/',$uri);

        $params = "ec_url_allow=".$uri."&ec_proto_allow=http,https&ec_expire=" .$expiration;
        $token = watoken_generate(self::$key, $params);
        $hashedUrl=$uri . "?" . $token . "\n";
        return $hashedUrl;
    }

/**
 * getCdnUrl
 * This method allows you to grab your CDN Hostname from anywhere in your application
 *
 *
 * @return string
 *
 * @access public
 * @static
 */
    public static function getCdnUrl() {
        return self::$url;
    }

}

At this point you can use this in your blade template or anywhere in the application like this:

In model, controllers or anywhere using php code:

MyCdnHelper::cdnEncode("/css/style.css")

On blade templates:

{{MyCdnHelper::cdnEncode("/css/style.css")}}

 

NOTE: watoken_generate is a Webair PHP Module created to do the hashing faster. This is included if you sign up for the Webair CDN.

Related Articles

  • How to List Compiled PHP Modules from the Command Line

    The general command is php -m; this command will give you the full list of extensions: php -m This command will give you an output like: bz2 calendar Core ctype...
  • Using PHP with PDO

    Introduction Often times during the development process of a dynamic web site, it will become necessary to pull information stored in a local or off-site database. Whether it is MySQL,...
  • Change CDN DNS

    If in any case you would want to change your DNS for CDN usage, please provide your webair account credentials. Having your credentials will proceed you into doing the next...
  • Understand My CDN

      Our CDN technology provides unparalleled throughput and responsiveness — video plays smoothly without stalling or choppiness,audio files start instantly, as if they were on your hard drive and software...