better-staridc-MNBT
1<?php
2if (!defined('IN_CRONLITE')) {
3 exit;
4}
5
6mnbt_plugin_register('home_demo', ['name' => '首页接管示例']);
7
8// 后台菜单
9mnbt_register_menu('admin', [
10 'title' => '首页接管示例',
11 'page' => 'index',
12 'icon' => 'mdi-home',
13 'order' => 25,
14 'multitabs' => true,
15]);
16
17// 后台设置页
18mnbt_register_page('admin', 'index', 'admin/index.php', '首页接管示例');
19
20// 插件管理页快捷入口
21mnbt_register_settings_tab([
22 'title' => '首页接管设置',
23 'page' => 'index',
24 'order' => 30,
25]);
26
27// AJAX:保存配置
28mnbt_register_ajax('admin', 'p_home_demo_save', function () {
29 mnbt_plugin_require_admin();
30 $mode = isset($_POST['mode']) ? (string)$_POST['mode'] : 'off';
31 if (!in_array($mode, ['off', 'redirect', 'render'], true)) {
32 json_exit_error('模式无效');
33 }
34 $target = isset($_POST['redirect_target']) ? trim((string)$_POST['redirect_target']) : '/user';
35 if ($mode === 'redirect' && $target === '') {
36 json_exit_error('重定向目标不能为空');
37 }
38 if (mb_strlen($target) > 500) {
39 json_exit_error('重定向目标过长');
40 }
41 mnbt_plugin_option_set('home_demo', 'mode', $mode);
42 mnbt_plugin_option_set('home_demo', 'redirect_target', $target);
43 json_exit_success('已保存', ['mode' => $mode]);
44});
45
46// ============================================================
47// 首页接管:根据后台配置决定 / 的响应
48// ============================================================
49mnbt_register_home(function ($ctx) {
50 $mode = mnbt_plugin_option_get('home_demo', 'mode', 'off');
51
52 if ($mode === 'off') {
53 // 不接管,回退到默认 /user 跳转
54 return false;
55 }
56
57 if ($mode === 'redirect') {
58 // 返回字符串 → 引擎自动 header("Location: ...") + exit
59 $target = mnbt_plugin_option_get('home_demo', 'redirect_target', '/user');
60 return $target;
61 }
62
63 if ($mode === 'render') {
64 // 直接渲染自定义首页
65 $tpl = mnbt_plugin_path('home_demo') . 'user/home.php';
66 if (is_file($tpl)) {
67 // 这些变量在模板中可能用到
68 $info = $ctx;
69 include $tpl;
70 } else {
71 header('Content-Type: text/html; charset=UTF-8');
72 echo '<!doctype html><meta charset="utf-8"><title>首页</title>';
73 echo '<h1>首页接管示例</h1><p>模板文件缺失:user/home.php</p>';
74 }
75 return true; // 已渲染,引擎 exit
76 }
77
78 return false;
79});
80
81// ============================================================
82// 通用路由:/landing 活动落地页演示
83// ============================================================
84mnbt_register_route('GET', '/landing', function ($params, $ctx) {
85 $tpl = mnbt_plugin_path('home_demo') . 'user/landing.php';
86 if (is_file($tpl)) {
87 $info = $ctx;
88 include $tpl;
89 } else {
90 header('Content-Type: text/html; charset=UTF-8');
91 echo '<!doctype html><meta charset="utf-8"><title>落地页</title>';
92 echo '<h1>活动落地页</h1><p>模板文件缺失:user/landing.php</p>';
93 }
94 // 不返回 → 视为已处理,引擎 exit
95});
96
97// 带参数的路由演示:/promo/{id}
98mnbt_register_route('GET', '/promo/{id}', function ($params, $ctx) {
99 header('Content-Type: text/html; charset=UTF-8');
100 $id = isset($params['id']) ? $params['id'] : '';
101 $base = isset($ctx['base']) ? $ctx['base'] : '';
102 echo '<!doctype html><meta charset="utf-8"><title>推广 ' . htmlspecialchars($id, ENT_QUOTES, 'UTF-8') . '</title>';
103 echo '<body style="font-family:sans-serif;padding:2rem;line-height:1.6">';
104 echo '<h1>推广页</h1>';
105 echo '<p>推广 ID:<code>' . htmlspecialchars($id, ENT_QUOTES, 'UTF-8') . '</code></p>';
106 echo '<p>这是 <code>mnbt_register_route(\'GET\', \'/promo/{id}\', ...)</code> 的演示。</p>';
107 echo '<p><a href="' . htmlspecialchars($base . '/', ENT_QUOTES, 'UTF-8') . '">返回首页</a></p>';
108 echo '</body>';
109});