That should be covered here https://laravel.com/docs/5.4/filesystem#configuration
Storage dependency injection
Hi,
I created a new local disk for my filesystem
I have many controllers where I have to repeat the Storage::disk("shared") declaraton. however i would like to stay DRY.
So my idea was to inject the Filesystem instance and use it like this
public function index (SharedFilesystem $disk)
{
return $disk->files('/');
}
But i can't find a good way to bind my SharedFilesystem in the Service Container. Do you have any idea ?
I still use laravel 5.4 for this project.
@cobs You can find the corresponding interface you need to type-hint for each facade here: https://laravel.com/docs/8.x/facades#facade-class-reference
For storage, you can either type-hint the manager (which gives you access to all disks) using Illuminate\Filesystem\FilesystemManager or an instance of the default driver by type-hinting Illuminate\Contracts\Filesystem\Filesystem.
If you want to inject a non-default disk into your controllers, then you’ll need to use the manager and then access the named disk via that:
class FooController extends Controller
{
protected $disk;
public function __construct(FilesystemManager $manager)
{
$this->disk = $manager->disk('shared');
}
}
If you don’t want to have to specify the name of the disk then you’ll need to add a contextual binding to the container:
$this->app->when(FooController::class)
->needs(Filesystem::class)
->give(function () {
return $this->app['filesystem']->driver('shared');
});
However, you’ll just make your life easier if you just use the default disk.
Please or to participate in this conversation.