Home » Bunny CDN Laravel – 5 Simplified Steps to Integrate It

Bunny CDN Laravel – 5 Simplified Steps to Integrate It

Loved it? Please Share on Social Media

INTRODUCTION:

Hello, fellow developers. What’s up?, I hope you are doing fine. Welcome back to the bunny CDN Laravel tutorial. In this extended tutorial, we will learn how we can use bunny cdn in our laravel application.

On the internet, the resources for integrating bunny cdn in the laravel application are very limited. I didn’t find any very good amount of resources that I was looking for. After that, I found a package called PlatformCommunity/flysystem-bunnycdn by sifex. I’ve tested the package very deeply and it works fine on my project.

Now, Let’s start our in-depth tutorial. Please follow each step very carefully so that you can get the 100& from this tutorial. 

What is Bunny CDN?:

Bunny CDN is a Content Delivery Network. It is used to deliver your web assets like images, CSS, and JS files via their CDN Servers. Also, it helps web applications reduce latency and improve load times. So that your user can get a great experience with your web application.

Getting Bunny CDN Credentials:

Well, Before all of that make sure you have an account on bunny cdn. You can get 14 14-day trial from bunny cdn. We need some information regarding bunny cdn, I’m assuming that you have an active bunny cdn account. After that, you have to create your storage zone and pick up your API key & Region.

Install The Bunny CDN Laravel Package by SIFEX:

Next, we will install our PlatformCommunity/flysystem-bunnycdn package for the laravel application. Copy & paste the following command into your terminal & hit enter.

composer require platformcommunity/flysystem-bunnycdn "*"

Quick Note: To install flysystem-bunnycdn, use the package without a version limitation. It should match the flysystem-bunnycdn version to your FlySystem version (v2, 3, etc.).

Configure The Package:

We have to configure our flysystem-laravel package first. For that, you have to copy & paste the following code snippet into AppServiceProvider>boot method.

/**
     * Bootstrap any application services.
     *
     * @return void
     */

    use Illuminate\Support\Facades\Storage;
    use League\Flysystem\Filesystem;
    use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNAdapter;
    use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNClient;
    use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNRegion;
   
    public function boot()
    {
        Storage::extend('bunnycdn', function ($app, $config) {
            $adapter = new BunnyCDNAdapter(
                new BunnyCDNClient(
                    $config['storage_zone'],
                    $config['api_key'],
                    $config['region']
                ),
                'http://testing.b-cdn.net' # Optional
            );

            return new FilesystemAdapter(
                new Filesystem($adapter, $config),
                $adapter,
                $config
            );
        });
    }

Configuring Flysystem for Bunny CDN Laravel Package:

Also, we need to configure our config/filesystems.php file for our package. copy & paste the following code into that file.

'bunnycdn' => [
            'driver' => 'bunnycdn',
            'storage_zone' => env('BUNNYCDN_STORAGE_ZONE'),
            'api_key' => env('BUNNYCDN_API_KEY'),
            'region' => env('BUNNYCDN_REGION' \PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNRegion::DEFAULT)
        ],

Quick Note: You can change your desired CDN region inside your config/filesystems.php file. You can learn more by their Bunny CDN API documentation.

Configuring .ENV File for Bunny CDN Laravel Package:

Finally, we must set the BUNNYCDN_STORAGE_ZONEBUNNYCDN_API_KEY, and BUNNYCDN_REGION variables in our.env file. Please follow the below code for more learning.

BUNNYCDN_STORAGE_ZONE=testing_storage_zone
BUNNYCDN_API_KEY="api-key"
BUNNYCDN_REGION=uk

Using Bunny CDN in Laravel Controller:

Our all steps are completed successfully. Now, we can use our controller like the below example, please observe and practice the below code snippet for more learning.

use Illuminate\Support\Facades\Storage;
Storage::disk('bunnycdn')->put('index.html', '<html>Hello World</html>');
return response(Storage::disk('bunnycdn')->get('index.html'));

List of Regions Provided By Bunny CDN:

Bunny CDN currently provides nine regions with their CDN service. You can take a look the all regions so that you can use it on your demand.

# Europe
BunnyCDNRegion::FALKENSTEIN = 'de';
BunnyCDNRegion::STOCKHOLM = 'se';

# United Kingdom
BunnyCDNRegion::UNITED_KINGDOM = 'uk';

# USA
BunnyCDNRegion::NEW_YORK = 'ny';
BunnyCDNRegion::LOS_ANGELAS = 'la';

# SEA
BunnyCDNRegion::SINGAPORE = 'sg';

# Oceania
BunnyCDNRegion::SYDNEY = 'syd';

# Africa
BunnyCDNRegion::JOHANNESBURG = 'jh';

# South America
BunnyCDNRegion::BRAZIL = 'br';

Pro Tips:

If you are constructing any changes to the assets of your application. Always try to purge your Bunny CDN cache. So that, your users can get the latest change of your application. 

Another thing is, always try to test your laravel application using page speed tools like GTMetrix to ensure that your resources are coming through Bunny CDN.

CONCLUSION:

Finished! That’s all about our tutorial. I’ve tried to teach you the most updated & tested things in this tutorial. Thank you so much for reading the whole tutorial from the beginning. If this tutorial helps you a little bit, then remember to share this post on social media. If you have questions, suggestions, or tips regarding this post, let us know via our Contact Us page.

RANDOM FAQ’S:

How do I set up a bunny CDN in Laravel?

You can use the PlatformCommunity/flysystem-bunnycdn package for integrating cdn quickly into your laravel application.

Can I use Bunny CDN with Cloudflare?

Yeah, you can but it is not good practice. Always use one instead of using two.

What is the purpose of CDN?

It helps web applications reduce latency and improve load times. So that your user can get a great experience with your web application.

How does bunny CDN work?

It stores your web application assets permanently so that it will serve assets to your users amazingly fast.


Loved it? Please Share on Social Media

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.