Get your next remote job on LaraJobs.
1 contributor Edit on GitHub Laravel

How to create a custom filesystem adapter in Laravel

Table of contents:

How to create a custom filesystem adapter in Laravel

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.');
Benjamin Crozat

Written by Benjamin Crozat

Indie hacker, blogger, and AI enthusiast building things with the TALL stack. 🔥

Follow me on:

Recommended articles

20+ Laravel best practices, tips and tricks to use in 2023 20+ Laravel best practices, tips and tricks to use in 2023

Learning a framework can be overwhelming, but time and execution will make you a master. Here are some best practices to help you toward your goal.

Modified on Oct 17, 2023

A complete history of Laravel's versions (2011-2023) A complete history of Laravel's versions (2011-2023)

What's the current version of Laravel? Is there any LTS release you can rely on? And what about the history of the framework? Let's find out!

Modified on Oct 15, 2023

Laravel interview questions and answers for 2023 Laravel interview questions and answers for 2023

Nailing a Laravel job interview can be a daunting task, but with the right preparation and mindset, you can set yourself up for success.

Modified on Sep 19, 2023 Audio available

Laravel 10 is out! Here are every new features and changes. Laravel 10 is out! Here are every new features and changes.

Laravel 10 has been released on February 14, 2023. Let's dive into every relevant new features and changes.

Modified on Nov 2, 2023

Gold sponsors New

  • Wire Elements
    Beautiful handcrafted Livewire components.
    Check site
Your business here

Partners

If you buy from one of my partners below, I will be compensated at no cost to you. These are services I use or used, and 100% stand behind.

  • Scalable and reliable VPS hosting.
    Bonus: $200 of free credits
    Check site
  • The Google Analytics alternative without compromise.
    Free trial: 30 days
    Bonus: $10 off your first invoice
    Check site
  • Flare
    Track PHP and JavaScript errors in one place.
    Free trial: 10 days
    Check site
  • Keep track of your Google rankings.
    Free trial: 7 days
    Promo code: WELCOME30
    Check site
- / -