refactor(routes): canonical URLs now match sablog format exactly
- /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:
@@ -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();
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ class CommentsTable
|
||||
->label('文章')
|
||||
->limit(30)
|
||||
->description(fn ($record) => $record->post ? \Illuminate\Support\Str::limit($record->post->excerpt_or_fallback, 50) : null)
|
||||
->url(fn ($record) => $record->post ? route('posts.show', $record->post->slug ?: $record->post->id) : null)
|
||||
->url(fn ($record) => $record->post ? route('posts.show', $record->post->id) : null)
|
||||
->openUrlInNewTab(),
|
||||
TextColumn::make('status')
|
||||
->label('状态')
|
||||
|
||||
@@ -110,7 +110,7 @@ class ApiController extends Controller
|
||||
'views' => $post->views,
|
||||
'comment_count' => $post->comment_count,
|
||||
'published_at' => $post->published_at?->toIso8601String(),
|
||||
'url' => route('posts.show', $post->slug ?: $post->id),
|
||||
'url' => route('posts.show', $post->id),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -237,7 +237,7 @@ class ApiController extends Controller
|
||||
'views' => $post->views,
|
||||
'comment_count' => $post->comment_count,
|
||||
'published_at' => $post->published_at?->toIso8601String(),
|
||||
'url' => route('posts.show', $post->slug ?: $post->id),
|
||||
'url' => route('posts.show', $post->id),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ class Category extends Model implements HasMedia
|
||||
|
||||
public function getUrlAttribute(): string
|
||||
{
|
||||
return route('category.show', $this->slug ?? $this->id);
|
||||
return route('category.show', $this->id);
|
||||
}
|
||||
|
||||
public function registerMediaCollections(): void
|
||||
|
||||
+1
-1
@@ -104,7 +104,7 @@ class Post extends Model implements HasMedia
|
||||
|
||||
public function getUrlAttribute(): string
|
||||
{
|
||||
return route('posts.show', $this->slug ?? $this->id);
|
||||
return route('posts.show', $this->id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user