在 Laravel 中动态编辑 config/database.php 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28405757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dynamically edit config/database.php file in Laravel
提问by sujit prasad
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.
请让我知道如何在 Laravel 中编辑 config/database.php 文件并更新用户提供的数据库凭据。
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => '*********',
'database' => '********',
'username' => '********',
'password' => '*******',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
回答by lukasgeiter
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.
您可能还想使用某种默认值database.php.dist
并database.php
在填充值时创建实际值。这样您就可以始终使用原始.dist
文件重复设置过程。
回答by Jirennor
If you want to set the database connection dynamically for just onerequest 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' => '',
),
),
回答by alexrussell
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!
这里没有内置功能可以执行您想要的操作。但是,我自己也做了类似的事情,我只是加载文件,执行字符串替换操作,然后将其写回。顺便说一下,这就是 laravel 自己的“设置应用程序密钥”命令的工作方式,所以至少我们没有遗漏任何未记录的功能!
Something like this, in your command's fire
method:
像这样的东西,在你的命令fire
方法中:
$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'));
}