How to create a custom filesystem adapter in Laravel

How to create a custom filesystem adapter in Laravel

Published
Aug 30, 2023
Written by
Benjamin Crozat
0
comments
2 minutes
read

Introduction

Laravel, with the help of Frank de Jonge’s Flysystem PHP package, offers a simple and consistent API to work with various filesystems like local, SFTP, Amazon S3, and more.

It also makes it a breeze to extend and offer new custom filesystems to your app. Let’s see how it works.

Create a custom filesystem adapter in Laravel, step by step

To create a filesystem adapter in Laravel, you will need to do the following:

  1. Create a new class that implements the League\Flysystem\FilesystemAdapter interface. This class, let say App\Filesystem\CustomAdapter, should implement all of the methods defined in the contract, such as write(), read(), delete(), and more.

  2. Once you have implemented all the methods of your custom filesystem adapter, you can register it with Laravel in app/Providers/AppServiceProvider.php

  3. Then, add an entry to the disks array in config/filesystems.php.

  4. Finally, you can use your custom file system adapter in your Laravel application. Leverage the Storage Facade for convenience.

OK, let’s see how all this looks like.

Here’s an example of what a custom file system adapter class looks like:

namespace App\Filesystem;

use League\Flysystem\Config;
use League\Flysystem\FilesystemAdapter;

class CustomAdapter implements FilesystemAdapter
{
    public function write(string $path, string $contents, Config $config) : void
    {
        //
    }

    public function read(string $path) : string
    {
        //
    }

    public function delete(string $path) : void
    {
        //
    }

    // There are more methods to implement.
}

Next, register your custom file system with Laravel:

namespace App\Providers;
 
use App\Filesystem\CustomAdapter;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\ServiceProvider;
use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Contracts\Foundation\Application;
 
class AppServiceProvider extends ServiceProvider
{
    public function boot() : void
    {
        Storage::extend('custom', function (Application $app, array $config) {
            return new FilesystemAdapter(
                new Filesystem(new CustomAdapter, $config),
                $adapter,
                $config
            );
        });
    }
}

Then, this is how you can register your custom filesystem adapter in the config/filesystems.php configuration file:

'disks' => [
    'custom' => [
        'driver' => App\Filesystem\CustomAdapter::class,
    ],
],

And finally, you can use your custom filesystem adapter in your Laravel code like this:

use Illuminate\Support\Facades\Storage;

Storage::disk('my-custom-driver')->put('lorem.txt', 'Lorem ipsum dolor sit amet.');
About Benjamin Crozat
Benjamin Crozat

Hi! I’m from the South of France and I’ve been a self-taught web developer since 2006. When I started learning PHP and JavaScript, PHP 4 was still widely used, Internet Explorer 6 ruled the world, and we used DHTML to add falling snow on websites.

Being able to educate myself for free on the web changed my life for the better. Giving back to the community was a natural direction in my career and I truly enjoy it.

Therefore, I decided to take action:

  1. I launched this blog in September 2022 with the goal to be in everyone’s Google search. I get more than tens of thousands of monthly clicks from it and even more visits overall (my analytics dashboard is public by the way).
  2. I also started growing my X (formerly Twitter) account at the same time, which has now over 7,000 followers.
  3. All the content I write is free thanks to my sponsors.

I also want to be completely free with my time and make a living with my own products. In April 2024, I launched Nobinge, a tool to summarize and chat with your content, including YouTube videos.

Believe me, I’m just getting started!

0 comments

You need to be signed in to comment this post.
Sign in with GitHub