Introduction
Laravel is one of the most popular and powerful PHP frameworks, known for its clean syntax and rich feature set. With an active community and widespread adoption, Laravel allows developers to efficiently build modern web applications. Some key features include:
- Simple installation and configuration.
- Modular architecture and easy extensibility.
- Robust ecosystem of packages, including Laravel's own suite of tools.
- Built-in database management, migrations, and seeding.
- Support for modern practices like RESTful routing and MVC architecture.
- Extensive documentation and a supportive community.
Integrating a Content Delivery Network (CDN) in Laravel enhances website performance by delivering assets from geographically distributed servers. This reduces latency, speeds up loading times, and improves the overall user experience. In this guide, we will walk you through the steps to integrate a CDN into your Laravel project.
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 cdn.mycompany.com Please visit the Services/How-To section to obtain your CDN domain.
- To integrate a CDN service on https:// website the CDN service must have 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/services.php file.
<?php return [ // other services... 'cdn' => [ 'enabled' => true, 'domain' => 'cdn.mycompany.com', 'protocol' => 'https', ], ];
Create CDN helper
Create a file app/Helpers/CdnHelper.php with the following content.
<?php namespace App\Helpers; use Illuminate\Support\Facades\Config; class CdnHelper { public static 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('services.cdn.enabled')) { return $url; } else { return Config::get('services.cdn.protocol') . '://' . Config::get('services.cdn.domain') . $url; } } }
Update autoloader
In modern Laravel, you should load helpers through a service provider. Create a service provider if you don’t have one. In app/Providers/HelperServiceProvider.php, add:
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; class HelperServiceProvider extends ServiceProvider { public function register() { // Register CdnHelper class $this->app->singleton('cdn', function ($app) { return new \App\Helpers\CdnHelper(); }); } public function boot() { // You can add additional bootstrapping logic here } }
Then, register the provider in config/app.php:
'providers' => [ // Other Service Providers... App\Providers\HelperServiceProvider::class, ],
Use the CDN helper in views
{{ \App\Helpers\CdnHelper::cdn('/image.jpg') }}
will output
https://cdn.mycompany.com/image.jpg
Update Composer Autoload
Run the following command to autoload the new helper class:
composer dump-autoload
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 the 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 trouble with custom fonts? Please refer to Using custom fonts with CDN - setting CORS for more details.
- Still having trouble? Check your CDN URLs using our content check tool or please refer to Debugging a CDN service for more hints.
- Can you improve this guide or write a new one for software we haven't covered? Help us expand our CDN integrations section and earn $50 in CDN credit! Contact us to contribute.