创建命令:
application/admin/command/ConfigRefresh.php
<?php
namespace app\admin\command;
use app\common\model\Config;
use think\console\Command;
use think\console\Input;
use think\console\Output;
class ConfigRefresh extends Command
{
protected function configure()
{
$this->setName('config_refresh')->setDescription('刷新配置缓存');
}
protected function execute(Input $input, Output $output)
{
$config = [];
$configList = Config::all();
foreach ($configList as $k => $v) {
$value = $v->toArray();
if (in_array($value['type'], ['selects', 'checkbox', 'images', 'files'])) {
$value['value'] = explode(',', $value['value']);
}
if ($value['type'] == 'array') {
$value['value'] = (array)json_decode($value['value'], true);
}
$config[$value['name']] = $value['value'];
}
file_put_contents(
CONF_PATH . 'extra' . DS . 'site.php',
'<?php' . "\n\nreturn " . var_export_short($config) . ";\n"
);
$output->writeln('ok.');
}
}
配置 command.php
文件:
application/command.php
<?php
return [
...
'app\admin\command\ConfigRefresh',
];
使用:
$ php think config_refresh
ok.
THE END