Add headless CMS for official site content management

Integrate article and page management into the Laravel admin dashboard
to serve as a headless CMS for the Next.js frontend (usher-site).

Backend:
- 7 migrations: article_categories, article_tags, articles, pivots, attachments, pages
- 5 models with relationships: Article, ArticleCategory, ArticleTag, ArticleAttachment, Page
- 4 admin controllers: articles (with publish/archive/pin), categories, tags, pages
- Admin views with EasyMDE markdown editor, multi-select categories/tags
- Navigation section "官網管理" in admin sidebar

API (v1):
- GET /api/v1/articles (filtered by type, category, tag, search; paginated)
- GET /api/v1/articles/{slug} (with related articles)
- GET /api/v1/categories
- GET /api/v1/pages/{slug} (with children)
- GET /api/v1/homepage (aggregated homepage data)
- Attachment download endpoint
- CORS configured for usher.org.tw, vercel.app, localhost:3000

Content migration:
- ImportHugoContent command: imports Hugo markdown files as articles/pages
- Successfully imported 27 articles, 17 categories, 11 tags, 9 pages

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-07 11:58:22 +08:00
parent bfbec861d0
commit a30af8eaf7
45 changed files with 4816 additions and 31 deletions

View File

@@ -0,0 +1,94 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Resources\ArticleCollectionResource;
use App\Http\Resources\ArticleResource;
use App\Models\Article;
use App\Models\ArticleAttachment;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class ArticleController extends Controller
{
public function index(Request $request)
{
$query = Article::with(['categories', 'tags'])
->active()
->forAccessLevel()
->orderByDesc('is_pinned')
->orderByDesc('published_at');
if ($request->filled('type')) {
$query->byContentType($request->type);
}
if ($request->filled('category')) {
$query->whereHas('categories', function ($q) use ($request) {
$q->where('article_categories.slug', $request->category);
});
}
if ($request->filled('tag')) {
$query->whereHas('tags', function ($q) use ($request) {
$q->where('article_tags.slug', $request->tag);
});
}
if ($request->filled('search')) {
$search = $request->search;
$query->where(function ($q) use ($search) {
$q->where('title', 'like', "%{$search}%")
->orWhere('content', 'like', "%{$search}%");
});
}
$articles = $query->paginate($request->integer('per_page', 12));
return ArticleCollectionResource::collection($articles);
}
public function show(string $slug)
{
$article = Article::with(['categories', 'tags', 'attachments'])
->active()
->forAccessLevel()
->where('slug', $slug)
->firstOrFail();
$article->incrementViewCount();
// Get related articles (same content_type, excluding current)
$related = Article::active()
->forAccessLevel()
->where('content_type', $article->content_type)
->where('id', '!=', $article->id)
->orderByDesc('published_at')
->limit(4)
->get();
return response()->json([
'data' => new ArticleResource($article),
'related' => ArticleCollectionResource::collection($related),
]);
}
public function downloadAttachment(string $slug, int $attachmentId)
{
$article = Article::active()
->forAccessLevel()
->where('slug', $slug)
->firstOrFail();
$attachment = ArticleAttachment::where('article_id', $article->id)
->findOrFail($attachmentId);
$attachment->incrementDownloadCount();
return Storage::disk('public')->download(
$attachment->file_path,
$attachment->original_filename
);
}
}

View File

@@ -0,0 +1,81 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Resources\ArticleCollectionResource;
use App\Http\Resources\CategoryResource;
use App\Http\Resources\PageResource;
use App\Models\Article;
use App\Models\ArticleCategory;
use App\Models\Page;
class HomepageController extends Controller
{
public function index()
{
// Featured/pinned articles
$featured = Article::with(['categories', 'tags'])
->active()
->forAccessLevel()
->pinned()
->orderBy('display_order')
->limit(5)
->get();
// Latest articles by type
$latestBlog = Article::with(['categories'])
->active()
->forAccessLevel()
->byContentType('blog')
->orderByDesc('published_at')
->limit(6)
->get();
$latestNotice = Article::with(['categories'])
->active()
->forAccessLevel()
->byContentType('notice')
->orderByDesc('published_at')
->limit(4)
->get();
$latestDocument = Article::with(['categories'])
->active()
->forAccessLevel()
->byContentType('document')
->orderByDesc('published_at')
->limit(4)
->get();
$latestRelatedNews = Article::with(['categories'])
->active()
->forAccessLevel()
->byContentType('related_news')
->orderByDesc('published_at')
->limit(4)
->get();
// About page
$aboutPage = Page::published()
->where('slug', 'about')
->first();
// Categories
$categories = ArticleCategory::withCount(['articles' => function ($q) {
$q->active()->forAccessLevel();
}])
->orderBy('sort_order')
->get();
return response()->json([
'featured' => ArticleCollectionResource::collection($featured),
'latest_blog' => ArticleCollectionResource::collection($latestBlog),
'latest_notice' => ArticleCollectionResource::collection($latestNotice),
'latest_document' => ArticleCollectionResource::collection($latestDocument),
'latest_related_news' => ArticleCollectionResource::collection($latestRelatedNews),
'about' => $aboutPage ? new PageResource($aboutPage) : null,
'categories' => CategoryResource::collection($categories),
]);
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Resources\PageResource;
use App\Models\Page;
class PageController extends Controller
{
public function show(string $slug)
{
$page = Page::published()
->with(['children' => function ($q) {
$q->published()->orderBy('sort_order');
}])
->where('slug', $slug)
->firstOrFail();
return new PageResource($page);
}
}