Update code igniter config file during installation

99封情书 提交于 2019-12-25 07:42:51

问题


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.

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?


回答1:


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);
}      


来源:https://stackoverflow.com/questions/17190810/update-code-igniter-config-file-during-installation

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