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

  • 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,...
  • 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...
  • 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...

    What is a LAMP Stack? “LAMP stack is a popular open source web platform commonly used to run dynamic web sites and servers. It includes Linux, Apache, MySQL, and PHP/Python/Perl...