refactor(routes): canonical URLs now match sablog format exactly
Build and Deploy / verify (push) Successful in 1m6s
Build and Deploy / deploy (push) Successful in 41s

- /show-{id}.shtml (comments page: /show-{id}-{page}.shtml)
- /category-{cid}.shtml / /category-{cid}-{page}.shtml
- /archives-{YYYYMM}.shtml / /archives-{YYYYMM}-{page}.shtml
- /tag/{name}, /tagslist.shtml, /index-{page}.shtml
- slug 时代的 /posts/{slug} 等旧格式全部 301 到 sablog 格式
This commit is contained in:
2026-09-09 21:21:07 +00:00
parent f6a9a8ea50
commit f5987e7cc2
22 changed files with 113 additions and 70 deletions
+10 -1
View File
@@ -20,8 +20,17 @@ class ArchiveController extends Controller
return theme_view('archives', compact('archives'));
}
public function month(string $year, string $month)
/**
* sablog 格式:/archives-{YYYYMM}.shtml
*/
public function month(string $date)
{
if (! preg_match('/^(\d{4})(\d{2})$/', $date, $m)) {
abort(404);
}
[$year, $month] = [$m[1], $m[2]];
$start = Carbon::create((int) $year, (int) $month, 1)->startOfMonth();
$end = (clone $start)->endOfMonth();
+7 -7
View File
@@ -78,7 +78,7 @@ class LegacyController extends Controller
$tag = Tag::findFromString($name, 'post');
return $tag
? redirect()->route('tags.show', $tag->slug ?: $tag->name, 301)
? redirect()->route('tags.show', $tag->name, 301)
: abort(404);
}
@@ -152,7 +152,7 @@ class LegacyController extends Controller
app(\App\Blog\Support\PluginManager::class)->doAction('comment.created', $comment);
return redirect()->route('posts.show', $post->slug ?: $post->id)
return redirect()->route('posts.show', $post->id)
->with($status === 'published' ? 'success' : 'error', $status === 'published' ? '评论已发布' : '评论已提交,等待审核')
->withFragment('comments');
}
@@ -162,7 +162,7 @@ class LegacyController extends Controller
$post = Post::find($id);
return $post
? redirect()->route('posts.show', $post->slug ?: $post->id, 301)
? redirect()->route('posts.show', $post->id, 301)
: abort(404);
}
@@ -171,7 +171,7 @@ class LegacyController extends Controller
$category = Category::find($cid);
return $category
? redirect()->route('category.show', $category->slug ?: $category->id, 301)
? redirect()->route('category.show', $category->id, 301)
: abort(404);
}
@@ -185,8 +185,8 @@ class LegacyController extends Controller
$month = $m[2] ?? null;
return $month
? redirect()->route('archives.month', [$year, $month], 301)
: redirect()->route('archives.month', [$year, '01'], 301);
? redirect()->route('archives.month', $year.$month, 301)
: redirect()->route('archives.month', $year.'01', 301);
}
private function tagList(Request $request)
@@ -198,7 +198,7 @@ class LegacyController extends Controller
$tag = $tagId ? Tag::find((int) $tagId) : Tag::find((int) $request->query('id'));
return $tag
? redirect()->route('tags.show', $tag->slug ?: $tag->name, 301)
? redirect()->route('tags.show', $tag->name, 301)
: abort(404);
}
+8 -3
View File
@@ -14,17 +14,22 @@ class PostController extends Controller
{
}
public function show(string $slug)
public function show(string $id, ?string $page = null)
{
$post = Post::query()
->where('slug', $slug)
->orWhere('id', (int) $slug)
->where('id', (int) $id)
->orWhere('slug', $id)
->first();
abort_unless($post && $post->status === 'published', 404);
$this->ensureReadable($post);
// sablog 式 /show-{id}-{page}.shtml 的评论分页
if ($page !== null) {
\Illuminate\Pagination\Paginator::currentPageResolver(fn () => max(1, (int) $page));
}
// 浏览量由 /track-view 端点统计(页面整页缓存下控制器不执行,避免双计)
$comments = $post->comments()
+5 -2
View File
@@ -28,9 +28,12 @@ class TagController extends Controller
return theme_view('tags', compact('tags'));
}
public function show(string $slug)
/**
* sablog 格式:/tag/{name},按名称(而非 slug)解析
*/
public function show(string $name)
{
$tag = Tag::findFromString($slug, 'post');
$tag = Tag::findFromString(urldecode($name), 'post');
abort_unless($tag, 404);