FastAdmin 命令行刷新配置缓存(刷新site.php文件)

创建命令:

application/admin/command/ConfigRefresh.php

e5ef257e65213104

<?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

e2e3ec38eb213143

<?php

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

使用:

9b98a3a57b213241

$ php think config_refresh
ok.

原文地址:https://www.hongfs.cn/2021/09/php/thinkphp/fastadmin-refresh-the-configuration-cache-from-the-command-line/

 
THE END
喜欢就支持一下吧
点赞0 分享