有些时候,为了不直接把配置文件存入数据库,做调整的时候不需要再去打开项目一个文件一个文件的去修改配置文件。
那么我们就可以这么做
一、在config配置文件里新建个文件夹setting
二、在文件夹里建对应你想配置的配置文件,如上图例子。
三、新建个config控制器,然后在里面建一个方法,用作读写
public function param() { if ($this->request->isPost()) { $param = $this->request->param(); extraconfig($param, 'setting/'.$param['model']); return $this->result('',200,'设置成功'); } $model = input('model')?:'qiniu'; $list = Con::load('setting/'.$model,'$model'); return View::fetch('param_'.$model, ['list' => $list]); }
四、在common.php里建一个公共方法,用作写入
/** * 修改扩展配置文件 * @param array $arr 需要更新或添加的配置 * @param string $file 配置文件名(不需要后辍) * @return bool */ function extraconfig($arr = [], $file = 'website') { if (is_array($arr)) { $filename = $file .'.php'; $filepath = config_path() . $filename; if (!file_exists($filepath)) { $conf = "<?php return [];"; file_put_contents($filepath, $conf); } $conf = include $filepath; foreach ($arr as $key => $value) { $conf[$key] = $value; } $time = date('Y/m/d H:i:s'); $str = "<?php\r\n/**\r\n * Alvin的学习主页(https://www.alvinxiao.com).\r\n * $time\r\n */\r\nreturn [\r\n"; foreach ($conf as $key => $value) { $str .= "\t'$key' => '$value',"; $str .= "\r\n"; } $str .= '];'; file_put_contents($filepath, $str); return true; } else { return false; } }
五、在视图文件view里新建文件夹和对应的配置文件的html文件,如图
六、页面视图和具体实现提交修改方法,大家自己去实现,这里只展示试图例子
本文由37°5【https://www.alvinxiao.com 】【https://blog.alvinxiao.com】原创,转载请注明来源。请注意原创和打造和谐的网络环境,谢谢!