Laravel CDN integration in 2024

Introduction

Laravel stands out as one of the most esteemed PHP frameworks, renowned for its elegant syntax and powerful features. With a growing community and widespread adoption, Laravel empowers developers to build robust web applications efficiently. This framework is favored for its:

  • Streamlined installation and configuration processes.
  • Modular and extensible architecture.
  • Rich ecosystem of packages and libraries.
  • Built-in tools for database management and migrations.
  • Support for modern development practices like RESTful routing and MVC architecture.
  • Comprehensive documentation and active community support.

Integrating a Content Delivery Network (CDN) with Laravel can significantly optimize performance by distributing content closer to users. In the following sections, we offer detailed guidance on seamlessly integrating a CDN with Laravel applications.

Before you start

  • Before you take any steps please back up your files and database.
  • In the following, we will integrate a CDN service using the CDN domain 12345.r.cdnsun.net. Please visit the Services/How-To section to obtain your CDN domain.
  • To integate a CDN service on https:// website you can use https://12345.r.cdnsun.net or you can use a custom CDN domain with SSL enabled.
  • If your website embeds custom fonts then please first enable CORS for them.
  • Before you take any steps please make sure that your CDN domain is ready-to-use here.

Create CDN Static service

Please refer to Creating a CDN Static service for more details.

Enable CDN in Laravel

Define CDN in config

Add the following to the Laravel's /config/app.php file.

<?php

return [
...
    'cdn_enabled'   => true,
    'cdn_domain'    => '12345.r.cdnsun.net',
    'cdn_protocol'  => 'https',
];

Create CDN helper

Create a file app/helpers.php with the following content.

<?php

function cdn($url = null)
{
    $url = (string) $url;
    if(empty($url))
    {
        throw new Exception('URL missing');
    }

    $pattern = '|^http[s]{0,1}://|i';        
    if(preg_match($pattern, $url))
    {
        throw new Exception('Invalid URL. ' .
            'Use: /image.jpeg instead of full URI: ' .
            'https://domain.com/image.jpeg.'
        );
    }
        
    $pattern = '|^/|';        
    if(!preg_match($pattern, $url))
    {
        $url = '/' . $url;
    }

    if(!Config::get('app.cdn_enabled'))
    {
        return $url;
    }
    else
    {
        return Config::get('app.cdn_protocol') . '://' . Config::get('app.cdn_domain') . $url;
    }    
}   

Update autoloader

Add the following to the Laravel's composer.json file under the key autoload.

...
"autoload": {    
     ...
     "files": [
         "app/helpers.php"
     ]
},
...

and in terminal run the following command.

composer dump-autoload    

Use the CDN helper in views

{{ cdn('/image.jpg') }}

will output

https://12345.r.cdnsun.net/image.jpg

Notes

  • View HTML source code of your web pages to verify that you are using CDN, you should see source attribute of your images, CSS, JavaScript, etc. beginning with your CDN domain.
  • Don't see your CDN domain in source code of your web pages? If your website is using any cache plug-in/mechanism then you might want to clear/flush its cache.
  • Having troubles with custom fonts? Please refer to Using custom fonts with CDN - setting CORS for more details.
  • Still having troubles? Check your CDN URLs in our CDN content check or please refer to Debugging a CDN service for more hints.

Contact Us

 _    _     _____     _  __  
| || | ||  |  ___||  | |/ // 
| || | ||  | ||__    | ' //  
| \\_/ ||  | ||__    | . \\  
 \____//   |_____||  |_|\_\\ 
  `---`    `-----`   `-` --`