1

I'm trying to set filesystem configuration value dynamically in controller level. (I think it's almost impossible).

For example:

    'sftp' => [
        'driver' => 'sftp',
        'host' => env('SFTP_HOST'),
        'port' => intval(env('SFTP_PORT')),
        'username' => env('SFTP_USERNAME'),
        'password' => env('SFTP_PASSWORD'),
    ],

This is my SFTP disk configuration value in filesystems.php.

I will have host, port, username, password values dynamically from database table. (There will be multiple records.)

And I'm trying to connect File System using File Manager Package. (ie: https://github.com/alexusmai/laravel-file-manager)

When I used static values, it worked well.

Now I'm trying to set it dynamically in Controller level.

$myConfigArrayvalue = MyModel::find($id);
config(['filesystems.disk.sftp' => $myConfigArrayvalue);

When I dd(config('filesystems.disk.sftp')) in controller or view, it shows dynamically value.

but in File Manager Package (ServiceProvider), it was showing empty value so when I go to view page, it didn't work.

I think this is because ServiceProvider was called before Controller.

Can anyone please help me how to do this?

LoveCoding
  • 1,121
  • 2
  • 12
  • 33

1 Answers1

1

I just hit this issue recently, after some research, I found that we can use Illuminate\Filesystem\FilesystemManager to create the sftp connection dynamically.

Here is how I do it:

            $config = [
                         'driver'   => 'sftp',
                         'host'     => $host,
                         'port'     => $port,
                         'username' => $username,
                         'password' => $password,
                      ];
            $fsMgr = new FilesystemManager(app());
            $sftpDisk = $fsMgr->createSftpDriver($config);
            $sftpDisk->put($fileName, $fullTxt);
SkyClasher
  • 171
  • 2
  • 11