周淼(MillsonZhou)的个人博客,记录、收藏、分享

AutoBackup插件中的config.xml文件的作用

在AutoBackup的开发过程中,我使用了一个配置文件config.xml,该文件的作用我在插件发布中并没有提及,通过这篇文章来详细介绍一下该文件的作用及实现方式。

这里我将config.xml简化成:


1
2
3
4
<?xml version="1.0" encoding="UTF-8"?>
<config>
<circle>2</circle>
</config>

在插件的配置面板函数config(Typecho_Widget_Helper_Form $form)函数的代码简化为:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$config_file = dirname(__FILE__).'/config.xml'; //config.xml的绝对地址
$xml = simplexml_load_file($config_file);
$circle = new Typecho_Widget_Helper_Form_Element_Text('circle', null, (string)$xml->circle, _t('更新周期(天)'));
$form->addInput($circle->addRule('isInteger', _t('更新周期必须是纯数字')));
$request = Typecho_Request::getInstance();
if ($request->isPost()) {
/**
* 更新配置文件
*/
$xml->circle = $request->get('circle');
$xml = $xml->asXML();
$fp = fopen($config_file, 'wb');
fwrite($fp, $xml); //向config.xml中写数据
fclose($fp);
}

$xml中的数据为:


1
2
3
4
object(SimpleXMLElement)#1 (11) {
["circle"]=>
string(1) "2"
}

在$circle的表单创建代码中可以看出,我是使用(string)$xml->circle来获取circle节点的数据的,为什么不直接用$xml->circle呢?我一开始是这样用的,但是报错,var_dump($xml->circle)的结果让我很意外:


1
2
3
4
object(SimpleXMLElement)#2 (1) {
[0]=>
string(1) "2"
}

实在是不得已啊

$request->isPost()用来判断是不是表单的提交,是的话就根据提交的数据来更新config.xml

以后更新插件,重新启用的时候就可以不用重新填写表单,而是从config.xml读取来进行初始化

日志信息 »

该日志于2010-09-03 16:02由 Millson 发表在程序开发分类下, 留言已关闭,但你可以将这个日志引用到你的网站或博客。

相关日志 »

  • 哇!恭喜您找到了一个独一无二的文章。

已有 3 条评论 »

  1. joyla joyla

    果断的沙发!

    1. 水煮鱼 水煮鱼

      我的沙发很多很多。。。

  2. 枫叶红秋雨 枫叶红秋雨

    博主,后台启用之后在设置页面出现

    Warning: simplexml_load_file() [function.simplexml-load-file]: /wwwroot/FTP1CEEE/htdocs/usr/plugins/AutoBackup/config.xml:1: parser error : Document is empty in /wwwroot/FTP1CEEE/htdocs/usr/plugins/AutoBackup/Plugin.php on line 45
    这个错误,怎么办
    服务器探针 www.fyhqy.com/tz.php

返回顶部