John Davidson

php - Laravel Dynamic Filesystem Configuration Set in Controller Level

0 comments
Message:


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?



My Answer:
> You can achieve this by using the `Config` facade in Laravel to dynamically set the filesystem configuration in the controller level. Here's an example of how you can do this:

php
use Illuminate\Support\Facades\Config;

public function setFilesystemConfig($driver)
{
Config::set('filesystems.disks.sftp.driver', $driver);
}


In this example, the `setFilesystemConfig` method takes the desired driver as a parameter and dynamically sets the `driver` configuration for the `sftp` disk in the filesystem configuration.

You can then call this method in your controller to dynamically set the filesystem configuration based on your requirements.

Rate this post

5 of 5 based on 2908 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us