laracoft's avatar

Disks for my package

If my package requires its own entries in config/filesystem.php, what is the best way to get them in place without requiring the developer to go through my README.md?

1 like
2 replies
martinbean's avatar

@laracoft When I’ve developed packages like this, I’ve made the disk configurable in the package’s own configuration file, and configurable via an environment variable, that has a sane default.

So, for an example package named Agora, it would have its own config/agora.php file:

return [
    'disk' => env('AGORA_DISK', env('FILESYSTEM_DISK')),
    'prefix' => env('AGORA_PREFIX', 'agora'),
];

It allows the developer to specify a disk using the AGORA_DISK environment variable, or falls back to the value of FILESYSTEM_DISK (the application’s default configured disk) by default.

In my package’s code, I’ll then upload files under a prefix, so that if the developer doesn’t configure a package-specific disk (and it uses the application’s default filesystem), then the possibility of a filename collision (and overwriting files) is reduced:

// Example code from a package controller...
$path = $request->file('image')->store(config('agora.prefix'), config('agora.disk'));

This will upload the image from the request to (by default) an /agora sub-directory in the configured disk.

1 like

Please or to participate in this conversation.