fix: 标签/文章/分类 slug 路由约束放开为 [^/]+,支持中文标签(如 /tags/随笔.shtml),.shtml 字面后缀保证不冲突

This commit is contained in:
ak
2026-08-11 23:05:10 +08:00
parent 7622cd8e2a
commit c02fb063c7
11 changed files with 16 additions and 6 deletions
+6 -6
View File
@@ -31,15 +31,15 @@ Route::get('/', function () {
return app(HomeController::class)->index(); return app(HomeController::class)->index();
})->name('home'); })->name('home');
Route::get('/posts/{slug}.shtml', [PostController::class, 'show'])->where('slug', '[A-Za-z0-9\-]+')->name('posts.show'); Route::get('/posts/{slug}.shtml', [PostController::class, 'show'])->where('slug', '[^/]+')->name('posts.show');
Route::get('/posts/{slug}', fn (string $slug) => redirect()->route('posts.show', $slug, 301))->where('slug', '[A-Za-z0-9\-]+'); Route::get('/posts/{slug}', fn (string $slug) => redirect()->route('posts.show', $slug, 301))->where('slug', '[^/]+');
Route::post('/posts/{post}/comments', [CommentController::class, 'store'])->name('comments.store'); Route::post('/posts/{post}/comments', [CommentController::class, 'store'])->name('comments.store');
// 浏览量统计(独立端点,避开整页缓存,保证实时) // 浏览量统计(独立端点,避开整页缓存,保证实时)
Route::get('/track-view/{post}', fn (\App\Models\Post $post) => response($post->increment('views') ? '' : '', 204))->name('track-view'); Route::get('/track-view/{post}', fn (\App\Models\Post $post) => response($post->increment('views') ? '' : '', 204))->name('track-view');
Route::get('/category/{slug}.shtml', [CategoryController::class, 'show'])->where('slug', '[A-Za-z0-9\-]+')->name('category.show'); Route::get('/category/{slug}.shtml', [CategoryController::class, 'show'])->where('slug', '[^/]+')->name('category.show');
Route::get('/category/{slug}', fn (string $slug) => redirect()->route('category.show', $slug, 301))->where('slug', '[A-Za-z0-9\-]+'); Route::get('/category/{slug}', fn (string $slug) => redirect()->route('category.show', $slug, 301))->where('slug', '[^/]+');
Route::get('/archives.shtml', [ArchiveController::class, 'index'])->name('archives'); Route::get('/archives.shtml', [ArchiveController::class, 'index'])->name('archives');
Route::get('/archives', fn () => redirect()->route('archives', [], 301)); Route::get('/archives', fn () => redirect()->route('archives', [], 301));
@@ -51,8 +51,8 @@ Route::get('/archives/{year}/{month}', fn (string $year, string $month) => redir
Route::get('/tags.shtml', [TagController::class, 'index'])->name('tags'); Route::get('/tags.shtml', [TagController::class, 'index'])->name('tags');
Route::get('/tags', fn () => redirect()->route('tags', [], 301)); Route::get('/tags', fn () => redirect()->route('tags', [], 301));
Route::get('/tags/{slug}.shtml', [TagController::class, 'show'])->where('slug', '[A-Za-z0-9\-]+')->name('tags.show'); Route::get('/tags/{slug}.shtml', [TagController::class, 'show'])->where('slug', '[^/]+')->name('tags.show');
Route::get('/tags/{slug}', fn (string $slug) => redirect()->route('tags.show', $slug, 301))->where('slug', '[A-Za-z0-9\-]+'); Route::get('/tags/{slug}', fn (string $slug) => redirect()->route('tags.show', $slug, 301))->where('slug', '[^/]+');
Route::get('/search.shtml', [SearchController::class, 'index'])->name('search'); Route::get('/search.shtml', [SearchController::class, 'index'])->name('search');
Route::get('/search', fn () => redirect()->route('search', [], 301)); Route::get('/search', fn () => redirect()->route('search', [], 301));