Files
laralog/app/Blog/Controllers/ArchiveController.php
T
gouki f5987e7cc2
Build and Deploy / verify (push) Successful in 1m6s
Build and Deploy / deploy (push) Successful in 41s
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 格式
2026-09-09 21:21:07 +00:00

47 lines
1.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Blog\Controllers;
use App\Models\Post;
use Carbon\Carbon;
class ArchiveController extends Controller
{
public function index()
{
$posts = Post::published()->orderByDesc('published_at')->get(['id', 'title', 'slug', 'published_at']);
$archives = $posts->groupBy(fn (Post $post) => $post->published_at->format('Y'))
->map(fn ($yearPosts) => $yearPosts->groupBy(fn (Post $post) => $post->published_at->format('m')));
return theme_view('archives', compact('archives'));
}
/**
* 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();
$posts = Post::query()
->whereBetween('published_at', [$start, $end])
->published()
->orderByDesc('published_at')
->paginate((int) blog_setting('per_page', config('blog.per_page')));
return theme_view('list', compact('posts'))
->with('archiveTitle', "{$year}{$month} 月");
}
}