08-设置页面与管理后台
# 第8章 设置页面与管理后台
## 8.1 前台设置页面
### 8.1.1 setting.php 标准模板
```php
'default1',
'option2' => 1,
'option3' => 0,
);
$input = array();
$input['option1'] = form_text('option1', $config['option1']);
$input['option2'] = form_radio_yes_no('option2', $config['option2']);
include _include(APP_PATH.'plugin/my_plugin/setting.htm');
} elseif ($method == 'POST') {
$config = setting_get('my_plugin');
$config['option1'] = param('option1');
$config['option2'] = param('option2', 0);
$config['option3'] = param('option3', 0);
setting_set('my_plugin', $config);
message(0, '设置成功');
}
}
```
### 8.1.2 多功能设置页
通过`param(3)`区分不同操作:
```php
0) {
message(1, '正在处理...', url('setting-my_plugin-rebuild-' . ($offset + 1000)));
} else {
message(0, '处理完成');
}
} else {
// 默认显示基本设置
// ...
}
```
### 8.1.3 setting.htm 标准模板
```php
```
### 8.1.4 表单函数
Xiuno BBS提供Bootstrap风格的表单生成函数:
```php
// 文本框
form_text('fieldname', $value);
// 单选(是/否)
form_radio_yes_no('fieldname', $value);
// 单选(自定义选项)
form_radio('fieldname', $options, $value);
// 下拉选择
form_select('fieldname', $options, $value);
// 复选框
form_checkbox('fieldname', $options, $checked);
// 文本域
form_textarea('fieldname', $value);
// 隐藏字段
form_hidden('fieldname', $value);
```
> **建议**: 表单函数灵活性较低,推荐直接编写HTML表单,使用Bootstrap 5样式。
## 8.2 后台管理路由
### 8.2.1 注册后台路由
通过`hook/admin_index_route_case_end.php`:
```php
-1), $page, 20);
include _include(APP_PATH.'plugin/my_plugin/view/htm/admin_list.htm');
} elseif ($action == 'edit') {
$id = param(2, 0);
if ($method == 'GET') {
$data = my_table_read($id);
include _include(APP_PATH.'plugin/my_plugin/view/htm/admin_edit.htm');
} elseif ($method == 'POST') {
$arr = array(
'field1' => param('field1'),
'field2' => param('field2', 0),
);
my_table_update($id, $arr);
message(0, '保存成功');
}
} elseif ($action == 'delete') {
$id = param(2, 0);
my_table_delete($id);
message(0, '删除成功');
}
```
### 8.2.3 后台权限检查
```php
// 管理员检查
if ($gid != 1) {
message(-1, '无权限访问');
}
// 超版/版主检查
if ($gid > 2) {
message(-1, '无权限访问');
}
```
## 8.3 后台导航菜单
### 8.3.1 添加后台菜单项
通过`hook/admin_index_start.php`或后台模板hook注入:
```php
'我的插件',
'url' => url('admin-myadmin'),
'icon' => 'fa-puzzle-piece',
);
```
### 8.3.2 后台页面模板
```php
```
## 8.4 耗时操作的分步处理
对于重建索引、批量处理等耗时操作,使用跳转方式分步执行:
```php
if ($action == 'rebuild') {
$offset = param(2, 0);
$pagesize = 1000;
$datalist = db_find('my_table', array(), array('id'=>1), $offset / $pagesize + 1, $pagesize);
if (empty($datalist)) {
message(0, '处理完成');
}
foreach ($datalist as $data) {
// 处理每条数据
my_process($data);
}
// 跳转到下一批
$next_offset = $offset + $pagesize;
message(1, "已处理 $next_offset 条...", url('setting-my_plugin-rebuild-' . $next_offset));
}
```
> **关键**: `message(1, ...)` 会触发页面跳转,实现分步执行。避免PHP超时。