在安装期间更新代码igniter配置文件 [英] Update code igniter config file during installation

查看:351
本文介绍了在安装期间更新代码igniter配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题多次,但几乎所有的回答告诉 save in db $ this-> config-> set_item

I know this question asked multiple times but almost all answer telling save in db or $this->config->set_item.

但这些答案不适用于我。我不是做一个网站,而是一个开源项目,并为非技术人员编写安装脚本,将在安装过程中为用户设置的一切。为此,我需要在安装过程中用用户特定的设置更新配置文件,如果用户需要,在后面的管理面板中更新。

However those answers do not apply for me. I'm not making a website but an open source project and working on the installation script for non technical persons, that will setup every thing for user during installation. For this I need to update config file with user specific settings during installation and form admin panel later, if user need.

显然,在DB中保存不适用,因为我甚至需要通过安装脚本在配置文件中写入数据库凭据。 $ this-> config-> set_item 也不适用,因为它仅为当前会话设置,我想永久设置。

Obviously saving in DB doesn't applicable as I even need to write DB credentials in config file through installation script. $this->config->set_item is also not applicable as it set only for current session and I want to set it permanently.

现在我正在创建配置文件模板和更新变量的想法。是正确的方式来实现要求吗?再次另一个问题在这里,如果用户更改一些设置从管理面板(说数据库凭据),更改不会立即反映。有没有任何解决方法或更好的解决方案?

Right now I'm working on idea of creating config file templates and update variables there. Is it right way to achieve requirements? Again another problem here, if user change some setting from admin panel (say database credentials), changes will not be reflected immediately. Is there any workaround or better solution for that?

推荐答案

你可以只读配置文件像* .txt,解析和改变你所需要的。例如,您想要更改用于连接到数据库的用户名(我不检查此代码,这只是一个示例):

You can just read config files as common files like *.txt, then parse and change what you need. For example, you want to change username for connection to database (I don't check this code, it's just an example):

$out = '';
$pattern = '$db[\'default\'][\'username\']';
$newValue = 'root';
$fileName = "application/config/database.php";
if(file_exists($fileName)) {
    $file = fopen($fileName,'r+');
    while(!feof($file)) { 
        $line = fgets($file);
        if(strpos($line, $pattern) !== false){
            $out = $pattern . " = '". $newValue . "'";
        }else{
            $out = $line;
        }
    }
    file_put_contents($file, $out);
    fclose($file);
}      

这篇关于在安装期间更新代码igniter配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆