2

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?

1
  • you are setting these config values in what controller? Commented Jul 30, 2022 at 3:12

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.