利用deepseek写一个简单的后台控件类,来简化一下后台的书写,利用这个类,我们可以把参数以数组的形式组装在一起,由类来轮询读取并生成控件。虽然这个对于站点的运行速度不会有什么正向的收益,但因为后台设置我们可以仅在管理员登陆的情况下才允许访问,所以也不会产生负担,当设置项较多时,数组形式的数据更为易读一些。

一个简单的后台实现类

if (!class_exists('Aug_Config')) {

    class Aug_Config {

        private $form;

        public function __construct($form) {
            $this->form = $form;
        }
    
        public function create_setting($items) {
            foreach ($items as $item) {
                $this->create_element($item);
            }
        }

        private function create_element($item) {
            // 基本参数提取
            $name = $item[0];
            $type = $item[1];
            $label = $item[2];
            $class = $item[3];
            $options = $item[4];
            $default = $item[5];
            $description = $item[6];
            $className = '\\Typecho\\Widget\\Helper\\Form\\Element\\' . $type;
            $element = new $className( $name, $options, $default, $label, $description );
            if (!empty($class)) {
                $currentClass = $element->getAttribute('class') ?? '';
                $element->setAttribute('class', trim("$currentClass $class"));
            }
            $this->form->addInput($element);
        }

    }

}

加上上面这个类之后,我们的后台设置页可以写成这样:

function themeConfig($form) {

    // 格式: [name, type, label, class, options, default, description]
    $settings = new Aug_Config($form);

    $settingsConfig = [
        [
            'Augfav',
            'Text',
            _t('站点 Favicon'),
            'aug-general',
            null,
            null,
            _t('显示在浏览器标签上的图标,仅支持 ico 或 png 格式,仅填写文件含后缀全名,图片请上传至主题 image 文件夹。')
        ],
        [
            'Augtongji',
            'Textarea',
            _t('站点统计'),
            'aug-general',
            null,
            null,
            _t('想知道站点的受访情况?亲可以在这里加入一段站点统计代码,统计代码一般由提供统计服务的站点给出,比如百度统计。')
        ],
        [
            'Augbanner',
            'Radio',
            _t('站点 Logo 展现形式'),
            'aug-general',
            [
                '0' => _t('仅文字'),
                '1' => _t('仅图片'),
                '2' => _t('图片+文字')
            ],
            '0',
            null
        ]
    ];
    
    $settings->create_setting($settingsConfig);
}
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。