Files
laralog/app/Blog/Controllers/ArchiveController.php
T
goukiandCursor ae47447062
Build and Deploy / verify (push) Successful in 40s
Build and Deploy / deploy (push) Successful in 36s
feat: restore sablog browse modes and clean imported HTML
Keep the public site readable after import (nbsp, highlighter, wrapping code, compact pager) and finish the admin extras already in this tree: sticky levels, preview, page cache, and live llms.txt.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-18 21:33:02 +00:00

48 lines
1.2 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 = blog_paginate(
Post::query()
->whereBetween('published_at', [$start, $end])
->published()
->orderByDesc('published_at')
);
return theme_view('list', compact('posts'))
->with('archiveTitle', "{$year} 年 {$month} 月");
}
}