From a0df3e60de9cfc19a0a7f42722e26ba2dd4207f0 Mon Sep 17 00:00:00 2001 From: ChatGPT Date: Sat, 28 Nov 2026 02:06:27 +0800 Subject: [PATCH] Allow deleting and recreating the homepage --- README.md | 2 +- app/Content/Database.php | 8 ++++---- app/Content/PageDraft.php | 2 +- app/Content/Repository.php | 3 --- app/Content/SiteBackup.php | 6 +++--- app/Controller/AdminController.php | 2 +- app/Controller/SiteController.php | 2 +- app/view/admin-dashboard.php | 21 ++++++++++++--------- app/view/admin-page.php | 2 +- app/view/missing.php | 8 ++++++-- app/view/studio-new-page.php | 2 +- 11 files changed, 31 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 3f5cde5..e048274 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ php start.php start 选中文字后可调整字号、字距、行距、字形和颜色。选中支持背景的容器或组件后,可以选择纯色、线性渐变或背景图。文字颜色和背景色选择器均提供当前配色方案的色样。样式以经过校验的 CSS 类保存,内容中的内联 `style` 属性会被清理。网页和画布按需加载样式。 -在「网站页面」可删除首页以外的页面。在「文章分类」删除分类会保留文章,并从动态组件的展示范围移除该分类。 +在「网站页面」可删除页面。删除首页后,访问 `/` 会显示站点的 404 页面。要重建首页,新建网址标识为 `home` 的普通页面并发布即可。在「文章分类」删除分类会保留文章,并从动态组件的展示范围移除该分类。 ### 站点图标与 Head diff --git a/app/Content/Database.php b/app/Content/Database.php index 9c62266..e123ef0 100644 --- a/app/Content/Database.php +++ b/app/Content/Database.php @@ -64,11 +64,11 @@ final class Database } } $pdo->prepare('INSERT OR IGNORE INTO site_settings (id, payload) VALUES (1, ?)')->execute([self::json(['name' => '我的网站'])]); - $pdo->prepare("INSERT OR IGNORE INTO pages - (slug, name, title, description, heading, accent, lead, image, alt, actions, quicklinks) - VALUES ('home', '首页', '我的网站', '欢迎访问我的网站。', '我的网站', '', '', '', '', '[]', '[]')") - ->execute(); if ($version === 0) { + $pdo->prepare("INSERT OR IGNORE INTO pages + (slug, name, title, description, heading, accent, lead, image, alt, actions, quicklinks) + VALUES ('home', '首页', '我的网站', '欢迎访问我的网站。', '我的网站', '', '', '', '', '[]', '[]')") + ->execute(); $articles = $pdo->prepare("INSERT OR IGNORE INTO pages (slug, name, title, description, heading, accent, lead, image, alt, actions, quicklinks) VALUES ('articles', '文章与动态', '文章与动态', '浏览站点发布的文章。', '文章与动态', '', '', '', '', '[]', '[]')"); diff --git a/app/Content/PageDraft.php b/app/Content/PageDraft.php index dc4b6fa..eee5202 100644 --- a/app/Content/PageDraft.php +++ b/app/Content/PageDraft.php @@ -8,7 +8,7 @@ use RuntimeException; final class PageDraft { - public const RESERVED = ['.', '.laysite', 'admin', 'assets', 'home', 'account', 'mcp', 'palette']; + public const RESERVED = ['.', '.laysite', 'admin', 'assets', 'account', 'mcp', 'palette']; public static function slug(mixed $value, array $reserved = self::RESERVED, bool $includeSpecial = false): string { diff --git a/app/Content/Repository.php b/app/Content/Repository.php index 402408b..c70bca8 100644 --- a/app/Content/Repository.php +++ b/app/Content/Repository.php @@ -174,9 +174,6 @@ final class Repository public function deletePage(string $slug, int $version): bool { - if ($slug === 'home') { - throw new RuntimeException('首页是站点入口,不能删除。'); - } return Database::write(function (PDO $pdo) use ($slug, $version): bool { $page = $this->one('SELECT pages.layout_id, d.kind AS layout_kind FROM pages LEFT JOIN design_presets d ON pages.layout_id = d.id WHERE pages.slug = ?', [$slug]); diff --git a/app/Content/SiteBackup.php b/app/Content/SiteBackup.php index 4b4a560..0e0711f 100644 --- a/app/Content/SiteBackup.php +++ b/app/Content/SiteBackup.php @@ -157,8 +157,8 @@ final class SiteBackup } } } - if (count($tables['site_settings']) !== 1 || !in_array('home', array_column($tables['pages'], 'slug'), true)) { - throw new RuntimeException('备份缺少站点身份或首页。'); + if (count($tables['site_settings']) !== 1) { + throw new RuntimeException('备份缺少站点身份。'); } $designs = []; foreach ($tables['design_presets'] as &$row) { @@ -198,7 +198,7 @@ final class SiteBackup $publishedSystemPages[$row['slug']] = $row['page_type']; } } elseif ($layout === null) { - PageDraft::slug($row['slug'] ?? null, array_diff(PageDraft::RESERVED, ['home'])); + PageDraft::slug($row['slug'] ?? null); } else { $kind = is_int($layout) ? ($designs[$layout] ?? null) : null; if (!in_array($kind, ['header', 'footer'], true) || ($row['slug'] ?? null) !== LayoutPage::slug($kind, $layout)) { diff --git a/app/Controller/AdminController.php b/app/Controller/AdminController.php index 310a493..d68d692 100644 --- a/app/Controller/AdminController.php +++ b/app/Controller/AdminController.php @@ -355,7 +355,7 @@ class AdminController $data['title'] = $data['name']; } $publication = $request->post('status'); - $data['status'] = $layout || $publication === null || $slug === 'home' ? $page['status'] : $this->input($request, 'status'); + $data['status'] = $layout || $publication === null ? $page['status'] : $this->input($request, 'status'); $scale = $this->input($request, 'image_scale'); $data['image_scale'] = $request->post('image_scale') === null ? (int) $page['image_scale'] : (ctype_digit($scale) ? (int) $scale : -1); diff --git a/app/Controller/SiteController.php b/app/Controller/SiteController.php index 235e8cb..fc62f09 100644 --- a/app/Controller/SiteController.php +++ b/app/Controller/SiteController.php @@ -241,7 +241,7 @@ final class SiteController return View::render('missing', [ 'site' => (new Repository())->site(), 'title' => '页面未找到', 'description' => '', - 'path' => null, 'noindex' => true, + 'path' => null, 'noindex' => true, 'atHomepage' => $path === '/', ], 404); } diff --git a/app/view/admin-dashboard.php b/app/view/admin-dashboard.php index f0d8708..201b056 100644 --- a/app/view/admin-dashboard.php +++ b/app/view/admin-dashboard.php @@ -47,7 +47,12 @@ foreach (['header', 'footer'] as $kind) { $type = $page['page_type']; $kind = $page['layout_kind'] ?? (isset(SystemPage::TYPES[$type]) ? $type : null); $label = $kind === null ? '' : ($pageTypeLabels[$kind] ?? ''); - $path = $page['slug'] === 'home' ? '/' : '/' . $page['slug']; + $home = $page['slug'] === 'home'; + $path = $home ? '/' : '/' . $page['slug']; + $deleteNote = '确定删除「' . $page['name'] . '」?此操作不可撤销。'; + if ($home) { + $deleteNote .= '删除首页后,访问 / 将显示 404 页面。'; + } $published = $page['status'] === 'published'; $public = $kind === null && $published; ?> @@ -65,14 +70,12 @@ foreach (['header', 'footer'] as $kind) { 'icon' => $public ? 'external' : 'preview', 'label' => ($public ? '查看公开页面' : '预览页面') . ' · ' . $page['name'], ]) ?> - -
- - - -
- +
+ + + +
diff --git a/app/view/admin-page.php b/app/view/admin-page.php index ff5e05a..afa33ea 100644 --- a/app/view/admin-page.php +++ b/app/view/admin-page.php @@ -314,7 +314,7 @@ $spacingPresets = [0, 4, 8, 12, 16, 24, 32, 48, 64, 96, 128];
- +
发布状态