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.
Before Laravel CDN integration, create a “New Resource” for your Small and Large objects or your Aksela account at https://cloud.medianova.com .
Create the ./App/helpers.php file and update the ./composer.json .
...
"autoload": {
"classmap": [
...
],
...
"files": [
"app/helpers.php"
]
},
...
Run the composer dump-autoload command from the terminal in the directory where your project is located.
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, "/");
}
?>
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"
),
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" />
Verify the HTML source code if your assets are loading from Medianova CDN.