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:

    //my custom paths (THIS)


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

//define the class
class MyCdnHelper {

    //define your CDN hostname
    protected static $url = "";

    //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:


On blade templates:



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

  • Add Domain to CDN

      You can add a domain the Webair CDN either in two ways, Customer Origin and CDN Origin. Customer Origin means that our Edge Nodes will be pulling requested content from...
  • 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...
  • Purging and Loading assets onto your CDN

    Step 1: Login to the Webair EZPanel ( Step 2: On the left side menu, click on CDN.   Step 3: Click on the “Webair CDN” button. This will open up...