Before you take any steps please back up your files and database.

The Laravel CDN integration could be done with either the CDN Assets Manager Package in combination with an AWS S3 bucket, the Simple CDN Package or by creating a helper that rewrites your URLs of your static assets. The following tutorial describe the creation of a helper in Laravel 5 to map your assets to the CDN URL.

1. Create a necessary CDN resource before beginning integration.

2. Create the ./app/helpers.php file and update the ./composer.json as follow:

...
"autoload": {
"classmap": [
...
],
...
"files": [
"app/helpers.php"
]
},
...
3. Execute the command composer dump-autoload to dump the autoloader.

4. Add the following code to your ./app/helpers.php:

<?php

// global CDN link helper function
function cdn( $asset ){

// Verify if CDN URLs are present in the config file
if( !Config::get('app.cdn') )
return asset( $asset );

// Get file name incl extension and CDN URLs
$cdns = Config::get('app.cdn');
$assetName = basename( $asset );

// Remove query string
$assetName = explode("?", $assetName);
$assetName = $assetName[0];

// Select the CDN URL based on the extension
foreach( $cdns as $cdn => $types ) {
if( preg_match('/^.*\.(' . $types . ')$/i', $assetName) )
return cdnPath($cdn, $asset);
}

// In case of no match use the last in the array
end($cdns);
return cdnPath( key( $cdns ) , $asset);

}

function cdnPath($cdn, $asset) {
return "//" . rtrim($cdn, "/") . "/" . ltrim( $asset, "/");
}
...
The standard asset() function will be called if no CDN URLs are defined in the ./config/app.php config file.

5. Define the CDN URLs in the ./config/app.php file

<?php

return [

/*
|--------------------------------------------------------------------------
| Application CDN domains
|--------------------------------------------------------------------------
|
| Specify different domains for your assets.
|
*/
'cdn' => array(
"cdn.example.com" => "css|js|eot|woff|ttf",
"img.example.com" => "jpg|jpeg|png|gif|svg",
"all.example.com" => ""
),

...
Type in your CDN Domain ot the CNAME that you specified in the SkyparkCDN control panel. Ensure that your CNAME record has been configured in a proper way befor using it for integration.

Example:



6. Use the global helper function in your views:

<img src="{{ cdn( "/img/yourImg.png" ) }}" alt="Your Image loaded from CDN" />


Integration has been completed! We highly recommend you to check the HTML code of your webpage to ensure that URLs have been rewritten properly from your original ones to CNAME from the control panel.

To do that press F12 or open Developers Tools in your browser, choose the Network tab and refresh the page. All static files should have your CNAME in URLs.