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
    // 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, "/");
    }
    ?>
  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 [
        /*
        |--------------------------------------------------------------------------
        | 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"
        ),
  6. The global use of the helper function is as follows
    <img src="{{ cdn( "/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

Was this post helpful?