做项目的时候我们有些数据可以不用写在数据库里面,比如网站的关键词,标题什么的,那么使用extra就很方便
首先在application创建一个extra文件,在里面创一个web.php文件,注意编码
然后创建一个控制器,比如controller/Config.php,在里面添加代码:
function add()
{
if (request()->isPost()) {
$path = 'config/extra/web.php';
$file = (include $path);
$config = array(
//网站配置
'WEB_COM' => input('WEB_COM')
);
$res = array_merge($file, $config);
$str = '<?php return [';
foreach ($res as $key => $value) {
$str .= '\'' . $key . '\'' . '=>' . '\'' . $value . '\'' . ',';
}
$str .= ']; ';
if (file_put_contents($path, $str)) {
$this->success('添加成功');
} else {
$this->error('添加失败');
}
}
}
其中WEB_COM就是你要添加的数据,最好是大写格式
<input type="text" name="WEB_COM" value="{:config('web.WEB_COM')}" placeholder="请输入内容">
直接用input方法添加数据,模板调用数据是{:config('web.WEB_COM')},控制器调用数据是config('web.WEB_COM')