FastAdmin 命令行刷新配置缓存

创建命令

application/admin/command/ConfigRefresh.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?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

1
2
3
4
5
6
<?php

return [
...
'app\admin\command\ConfigRefresh',
];

使用

1
2
$ php think config_refresh
ok.
往上