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 Zone” for your  Small and Large objects or your Aksela account at panel.medianova.com
  2. Create the ./App/helpers.php file and update the ./composer.json
    ...
    "autoload": {
        "classmap": [
          ...
         ],
         ...
         "files": [
             "app/helpers.php"
         ]
    },
    ...
    
  3. Run the composer dump-autoload command from the terminal in the directory where your project is located
  4. 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
    <?php
    
    function cdn( $file ){
    
        if( !Config::get('app.cdn') )
            return asset( $file );
    
        $cdns = Config::get('app.cdn');
        $fileName = basename( $file );
    
        $fileName = explode("?", $fileName);
        $fileName = $fileName[0];
    
        foreach( $cdns as $cdn => $types ) {
            if( preg_match('/^.*\.(' . $types . ')$/i', $fileName) )
                return cdnUrl($cdn, $file);
        }
    
        end($cdns);
        return cdnUrl( key( $cdns ) , $file);
    }
    function cdnUrl($cdn, $file) {
        return  "//" . rtrim($cdn, "/") . "/" . ltrim( $file, "/");
    }
    ?>
  5. Define the CDN URLs in the ./config/app.php file
    'cdn' => array(
            "your-cdn-url.com" => "css|js|eot|woff|ttf|jpg|jpeg|png|gif|svg"
        ),

    Example;

    <?php
    return [
        
    'cdn' => array(
            "your-cdn-url.com" => "css|js|eot|woff|ttf|jpg|jpeg|png|gif|svg"
        ),
  6. The global use of the helper function is as follows
    <img src="{{ cdn( "//img-docsmedianova.mncdn.com/img/medianovaCDN.png" ) }}" alt="Loaded from Medianova CDN" />
  7. Verify the HTML source code if your assets are loading from Medianova CDN

 

Note: You can use the https://docs.medianova.com/en/medianova-cdn-user-integration/ link to view the CDN URLs of your Zone and Aksela accounts you have opened for your Small, Large objects