Skip to main content

Laravel Medianova CDN Integration

Laravel Medianova CDN integration is done by creating a helper function that rewrites URLs.

We have prepared the following steps for Laravel CDN integration.

  1. Before Laravel CDN integration, create a “New Resource” for your  Small and Large objects or your Aksela account at https://cloud.medianova.com .

  2. Create the ./App/helpers.php file and update the ./composer.json .

CODE
...
"autoload": {
    "classmap": [
      ...
     ],
     ...
     "files": [
         "app/helpers.php"
     ]
},
...
  1. Run the composer dump-autoload command from the terminal in the directory where your project is located.

  2. Add the following code to your./app/helpers.php file. If the CDN URLs are not defined in the ./Config/app.php config file, the standard asset () function is called.

CODE
<?php
// global CDN function
function cdn( $file ){
    // Verify if Medianova CDN URLs are present in the config file
    if( !Config::get('app.cdn') )
        return asset( $file );
    // Get file name incl extension and CDN URLs
    $cdns = Config::get('app.cdn');
    $fileName = basename( $file );
    // Remove query string
    $fileName = explode("?", $fileName);
    $fileName = $fileName[0];
    // Select the CDN URL based on the extension
    foreach( $cdns as $cdn => $types ) {
        if( preg_match('/^.*\.(' . $types . ')$/i', $fileName) )
            return cdnUrl($cdn, $file);
    }
    // In case of no match use the last in the array
    end($cdns);
    return cdnUrl( key( $cdns ) , $file);
}
function cdnUrl($cdn, $file) {
    return  "//" . rtrim($cdn, "/") . "/" . ltrim( $file, "/");
}
?>
  1. Define the CDN URLs in the ./config/app.php file.

CODE
'cdn' => array(
        "your-cdn-url.com" => "css|js|eot|woff|ttf|jpg|jpeg|png|gif|svg"
    ),

Example:

CODE
<?php
return [
    /*
    |--------------------------------------------------------------------------
    | Application MnCDN domains
    |--------------------------------------------------------------------------
    |
    | Specify different domains for your assets.
    |
    */
'cdn' => array(
        "your-cdn-url.com" => "css|js|eot|woff|ttf|jpg|jpeg|png|gif|svg"
    ),
  1. The global use of the helper function is as follows.

CODE
<img src="{{ cdn( "//img-docsmedianova.mncdn.com/img/medianovaCDN.png" ) }}" alt="Loaded from Medianova CDN" />
  1. Verify the HTML source code if your assets are loading from Medianova CDN.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.