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,181 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\AuditLog;
use App\Models\Page;
use Illuminate\Http\Request;
class PageController extends Controller
{
public function __construct()
{
$this->middleware('can:view_pages')->only(['index', 'show']);
$this->middleware('can:create_pages')->only(['create', 'store']);
$this->middleware('can:edit_pages')->only(['edit', 'update']);
$this->middleware('can:delete_pages')->only(['destroy']);
$this->middleware('can:publish_pages')->only(['publish']);
}
public function index()
{
$pages = Page::with(['creator', 'parent'])
->topLevel()
->orderBy('sort_order')
->get();
// Also load child pages
$pages->load('children');
return view('admin.pages.index', compact('pages'));
}
public function create()
{
$parentPages = Page::topLevel()->orderBy('sort_order')->get();
return view('admin.pages.create', compact('parentPages'));
}
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'required|string|max:255',
'slug' => 'nullable|string|max:255|unique:pages,slug',
'content' => 'required|string',
'template' => 'nullable|string|max:255',
'status' => 'required|in:draft,published',
'meta_description' => 'nullable|string|max:500',
'parent_id' => 'nullable|exists:pages,id',
'sort_order' => 'nullable|integer',
]);
$page = Page::create([
'title' => $validated['title'],
'slug' => $validated['slug'] ?? null,
'content' => $validated['content'],
'template' => $validated['template'] ?? null,
'status' => $validated['status'],
'meta_description' => $validated['meta_description'] ?? null,
'parent_id' => $validated['parent_id'] ?? null,
'sort_order' => $validated['sort_order'] ?? 0,
'published_at' => $validated['status'] === 'published' ? now() : null,
'created_by_user_id' => auth()->id(),
'last_updated_by_user_id' => auth()->id(),
]);
AuditLog::create([
'user_id' => auth()->id(),
'action' => 'page.created',
'description' => "建立頁面:{$page->title} (狀態:{$page->getStatusLabel()})",
'ip_address' => request()->ip(),
]);
return redirect()
->route('admin.pages.show', $page)
->with('status', '頁面已成功建立');
}
public function show(Page $page)
{
$page->load(['creator', 'lastUpdatedBy', 'parent', 'children']);
return view('admin.pages.show', compact('page'));
}
public function edit(Page $page)
{
$parentPages = Page::topLevel()
->where('id', '!=', $page->id)
->orderBy('sort_order')
->get();
return view('admin.pages.edit', compact('page', 'parentPages'));
}
public function update(Request $request, Page $page)
{
$validated = $request->validate([
'title' => 'required|string|max:255',
'slug' => 'nullable|string|max:255|unique:pages,slug,'.$page->id,
'content' => 'required|string',
'template' => 'nullable|string|max:255',
'status' => 'required|in:draft,published',
'meta_description' => 'nullable|string|max:500',
'parent_id' => 'nullable|exists:pages,id',
'sort_order' => 'nullable|integer',
]);
$updateData = [
'title' => $validated['title'],
'content' => $validated['content'],
'template' => $validated['template'] ?? null,
'status' => $validated['status'],
'meta_description' => $validated['meta_description'] ?? null,
'parent_id' => $validated['parent_id'] ?? null,
'sort_order' => $validated['sort_order'] ?? 0,
'last_updated_by_user_id' => auth()->id(),
];
if (! empty($validated['slug']) && $validated['slug'] !== $page->slug) {
$updateData['slug'] = $validated['slug'];
}
if ($validated['status'] === 'published' && ! $page->published_at) {
$updateData['published_at'] = now();
}
$page->update($updateData);
AuditLog::create([
'user_id' => auth()->id(),
'action' => 'page.updated',
'description' => "更新頁面:{$page->title}",
'ip_address' => request()->ip(),
]);
return redirect()
->route('admin.pages.show', $page)
->with('status', '頁面已成功更新');
}
public function destroy(Page $page)
{
if ($page->children()->count() > 0) {
return back()->with('error', '此頁面下仍有子頁面,無法刪除');
}
$title = $page->title;
$page->delete();
AuditLog::create([
'user_id' => auth()->id(),
'action' => 'page.deleted',
'description' => "刪除頁面:{$title}",
'ip_address' => request()->ip(),
]);
return redirect()
->route('admin.pages.index')
->with('status', '頁面已成功刪除');
}
public function publish(Page $page)
{
if ($page->isPublished()) {
return back()->with('error', '此頁面已經發布');
}
$page->publish(auth()->user());
AuditLog::create([
'user_id' => auth()->id(),
'action' => 'page.published',
'description' => "發布頁面:{$page->title}",
'ip_address' => request()->ip(),
]);
return back()->with('status', '頁面已成功發布');
}
}