Dynamically edit config/database.php file in Laravel

不羁岁月 提交于 2019-12-20 14:38:39

问题


First time when user runs my application i want to set database details as entered by the user in my application.

Please let me know how i can edit config/database.php file in Laravel and update database credentials as provided by user.

'connections' => array(

    'mysql' => array(
        'driver'    => 'mysql',
        'host'      => '*********',
        'database'  => '********',
        'username'  => '********',
        'password'  => '*******',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),
),

回答1:


The simplest solution is probably to use placeholders in the initial config file:

'mysql' => array(
    'driver'    => 'mysql',
    'host'      => '%host%',
    'database'  => '%database%',
    'username'  => '%username%',
    'password'  => '%password%',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
),

And then just replace them with the actual values:

$path = app_path('config/database.php');
$contents = File::get($path);

$contents .= str_replace('%host%', $host, $contents);
// and so on

File::put($path, $contents);

This way you don't have to actually parse the file.

You might also want to use some kind of default database.php.dist and create the actual database.php when filling in the values. This way you could always repeat the set up process by using the original .dist file.




回答2:


If you want to set the database connection dynamically for just one request you can also use the following option.

Config::set('database.connections.local.host', '<New Host IP>');
Config::set('database.connections.local.database', '<New DB>');
Config::set('database.connections.local.username', '<New Username>');
Config::set('database.connections.local.password', '<New Password>');

Your DB config can looks like this in that case, or just provide default connection information and override it via the above commands.

'connections' => array(
    'local' => array(
        'driver'    => 'mysql',
        'host'      => '',
        'database'  => '',
        'username'  => '',
        'password'  => '',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_cli',
        'prefix'    => '',
     ),
),



回答3:


There's no built-in functionality to do what you want here. However, I have done a similar thing myself and I just load the file in, do a string replacement operation and then write it back out. This, by the way, is the way that laravel's own 'set application key' command works, so at least we aren't missing any undocumented functionality!

Something like this, in your command's fire method:

$dialog = $this->getHelperSet()->get('dialog');

$host = $dialog->ask($this->output, '<question>Hostname?</question> ');
$db   = $dialog->ask($this->output, '<question>Database?</question> ');
$user = $dialog->ask($this->output, '<question>Username?</question> ');
$pass = $dialog->ask($this->output, '<question>Password?</question> ');

if ($file = file_get_contents(app_path('config/database.php')) {
    $file = str_replace("'host'      => '*********'", "'host'      => '".$host."'", $file);
    $file = str_replace("'database'  => '********'", "'database'  => '".$db."'", $file);
    $file = str_replace("'username'  => '********'", "'username'  => '".$user."'", $file);
    $file = str_replace("'password'  => '*******'", "'password'  => '".$pass."'", $file);

    file_put_contents(app_path('config.database.php'));
}


来源:https://stackoverflow.com/questions/28405757/dynamically-edit-config-database-php-file-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!