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>
450 lines
16 KiB
PHP
450 lines
16 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Article;
|
|
use App\Models\ArticleAttachment;
|
|
use App\Models\ArticleCategory;
|
|
use App\Models\ArticleTag;
|
|
use App\Models\AuditLog;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
|
|
class ArticleController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware('can:view_articles')->only(['index', 'show']);
|
|
$this->middleware('can:create_articles')->only(['create', 'store']);
|
|
$this->middleware('can:edit_articles')->only(['edit', 'update']);
|
|
$this->middleware('can:delete_articles')->only(['destroy']);
|
|
$this->middleware('can:publish_articles')->only(['publish', 'archive']);
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$query = Article::with(['creator', 'categories'])
|
|
->orderByDesc('is_pinned')
|
|
->orderByDesc('created_at');
|
|
|
|
if ($request->filled('status')) {
|
|
$query->where('status', $request->status);
|
|
}
|
|
|
|
if ($request->filled('content_type')) {
|
|
$query->where('content_type', $request->content_type);
|
|
}
|
|
|
|
if ($request->filled('category')) {
|
|
$query->whereHas('categories', function ($q) use ($request) {
|
|
$q->where('article_categories.id', $request->category);
|
|
});
|
|
}
|
|
|
|
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(20);
|
|
|
|
$stats = [
|
|
'total' => Article::count(),
|
|
'draft' => Article::draft()->count(),
|
|
'published' => Article::published()->count(),
|
|
'archived' => Article::archived()->count(),
|
|
'pinned' => Article::pinned()->count(),
|
|
];
|
|
|
|
$categories = ArticleCategory::orderBy('sort_order')->get();
|
|
|
|
return view('admin.articles.index', compact('articles', 'stats', 'categories'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$categories = ArticleCategory::orderBy('sort_order')->get();
|
|
$tags = ArticleTag::orderBy('name')->get();
|
|
|
|
return view('admin.articles.create', compact('categories', 'tags'));
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'title' => 'required|string|max:255',
|
|
'slug' => 'nullable|string|max:255|unique:articles,slug',
|
|
'summary' => 'nullable|string|max:1000',
|
|
'content' => 'required|string',
|
|
'content_type' => 'required|in:blog,notice,document,related_news',
|
|
'access_level' => 'required|in:public,members,board,admin',
|
|
'author_name' => 'nullable|string|max:255',
|
|
'meta_description' => 'nullable|string|max:500',
|
|
'published_at' => 'nullable|date',
|
|
'expires_at' => 'nullable|date|after:published_at',
|
|
'is_pinned' => 'boolean',
|
|
'display_order' => 'nullable|integer',
|
|
'categories' => 'nullable|array',
|
|
'categories.*' => 'exists:article_categories,id',
|
|
'tags_input' => 'nullable|string',
|
|
'featured_image' => 'nullable|image|max:5120',
|
|
'featured_image_alt' => 'nullable|string|max:255',
|
|
'save_action' => 'required|in:draft,publish',
|
|
]);
|
|
|
|
$articleData = [
|
|
'title' => $validated['title'],
|
|
'slug' => $validated['slug'] ?? null,
|
|
'summary' => $validated['summary'] ?? null,
|
|
'content' => $validated['content'],
|
|
'content_type' => $validated['content_type'],
|
|
'access_level' => $validated['access_level'],
|
|
'author_name' => $validated['author_name'] ?? null,
|
|
'meta_description' => $validated['meta_description'] ?? null,
|
|
'status' => $validated['save_action'] === 'publish' ? Article::STATUS_PUBLISHED : Article::STATUS_DRAFT,
|
|
'published_at' => $validated['save_action'] === 'publish' ? ($validated['published_at'] ?? now()) : ($validated['published_at'] ?? null),
|
|
'expires_at' => $validated['expires_at'] ?? null,
|
|
'is_pinned' => $validated['is_pinned'] ?? false,
|
|
'display_order' => $validated['display_order'] ?? 0,
|
|
'created_by_user_id' => auth()->id(),
|
|
'last_updated_by_user_id' => auth()->id(),
|
|
'featured_image_alt' => $validated['featured_image_alt'] ?? null,
|
|
];
|
|
|
|
if ($request->hasFile('featured_image')) {
|
|
$articleData['featured_image_path'] = $request->file('featured_image')->store('articles/images', 'public');
|
|
}
|
|
|
|
$article = Article::create($articleData);
|
|
|
|
// Sync categories
|
|
if (! empty($validated['categories'])) {
|
|
$article->categories()->sync($validated['categories']);
|
|
}
|
|
|
|
// Handle tags
|
|
if (! empty($validated['tags_input'])) {
|
|
$tagNames = array_filter(array_map('trim', explode(',', $validated['tags_input'])));
|
|
$tagIds = [];
|
|
foreach ($tagNames as $tagName) {
|
|
$tag = ArticleTag::firstOrCreate(
|
|
['name' => $tagName],
|
|
['slug' => Str::slug($tagName) ?: 'tag-'.time()]
|
|
);
|
|
$tagIds[] = $tag->id;
|
|
}
|
|
$article->tags()->sync($tagIds);
|
|
}
|
|
|
|
AuditLog::create([
|
|
'user_id' => auth()->id(),
|
|
'action' => 'article.created',
|
|
'description' => "建立文章:{$article->title} (類型:{$article->getContentTypeLabel()},狀態:{$article->getStatusLabel()})",
|
|
'ip_address' => request()->ip(),
|
|
]);
|
|
|
|
$message = $validated['save_action'] === 'publish' ? '文章已成功發布' : '文章已儲存為草稿';
|
|
|
|
return redirect()
|
|
->route('admin.articles.show', $article)
|
|
->with('status', $message);
|
|
}
|
|
|
|
public function show(Article $article)
|
|
{
|
|
if (! $article->canBeViewedBy(auth()->user())) {
|
|
abort(403, '您沒有權限查看此文章');
|
|
}
|
|
|
|
$article->load(['creator', 'lastUpdatedBy', 'categories', 'tags', 'attachments']);
|
|
|
|
return view('admin.articles.show', compact('article'));
|
|
}
|
|
|
|
public function edit(Article $article)
|
|
{
|
|
if (! $article->canBeEditedBy(auth()->user())) {
|
|
abort(403, '您沒有權限編輯此文章');
|
|
}
|
|
|
|
$article->load(['categories', 'tags', 'attachments']);
|
|
$categories = ArticleCategory::orderBy('sort_order')->get();
|
|
$tags = ArticleTag::orderBy('name')->get();
|
|
|
|
return view('admin.articles.edit', compact('article', 'categories', 'tags'));
|
|
}
|
|
|
|
public function update(Request $request, Article $article)
|
|
{
|
|
if (! $article->canBeEditedBy(auth()->user())) {
|
|
abort(403, '您沒有權限編輯此文章');
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'title' => 'required|string|max:255',
|
|
'slug' => 'nullable|string|max:255|unique:articles,slug,'.$article->id,
|
|
'summary' => 'nullable|string|max:1000',
|
|
'content' => 'required|string',
|
|
'content_type' => 'required|in:blog,notice,document,related_news',
|
|
'access_level' => 'required|in:public,members,board,admin',
|
|
'author_name' => 'nullable|string|max:255',
|
|
'meta_description' => 'nullable|string|max:500',
|
|
'published_at' => 'nullable|date',
|
|
'expires_at' => 'nullable|date|after:published_at',
|
|
'is_pinned' => 'boolean',
|
|
'display_order' => 'nullable|integer',
|
|
'categories' => 'nullable|array',
|
|
'categories.*' => 'exists:article_categories,id',
|
|
'tags_input' => 'nullable|string',
|
|
'featured_image' => 'nullable|image|max:5120',
|
|
'featured_image_alt' => 'nullable|string|max:255',
|
|
'remove_featured_image' => 'boolean',
|
|
]);
|
|
|
|
$updateData = [
|
|
'title' => $validated['title'],
|
|
'summary' => $validated['summary'] ?? null,
|
|
'content' => $validated['content'],
|
|
'content_type' => $validated['content_type'],
|
|
'access_level' => $validated['access_level'],
|
|
'author_name' => $validated['author_name'] ?? null,
|
|
'meta_description' => $validated['meta_description'] ?? null,
|
|
'published_at' => $validated['published_at'],
|
|
'expires_at' => $validated['expires_at'] ?? null,
|
|
'is_pinned' => $validated['is_pinned'] ?? false,
|
|
'display_order' => $validated['display_order'] ?? 0,
|
|
'last_updated_by_user_id' => auth()->id(),
|
|
'featured_image_alt' => $validated['featured_image_alt'] ?? null,
|
|
];
|
|
|
|
if (! empty($validated['slug']) && $validated['slug'] !== $article->slug) {
|
|
$updateData['slug'] = $validated['slug'];
|
|
}
|
|
|
|
if ($request->hasFile('featured_image')) {
|
|
if ($article->featured_image_path) {
|
|
Storage::disk('public')->delete($article->featured_image_path);
|
|
}
|
|
$updateData['featured_image_path'] = $request->file('featured_image')->store('articles/images', 'public');
|
|
} elseif ($request->boolean('remove_featured_image') && $article->featured_image_path) {
|
|
Storage::disk('public')->delete($article->featured_image_path);
|
|
$updateData['featured_image_path'] = null;
|
|
}
|
|
|
|
$article->update($updateData);
|
|
|
|
// Sync categories
|
|
$article->categories()->sync($validated['categories'] ?? []);
|
|
|
|
// Handle tags
|
|
if (isset($validated['tags_input'])) {
|
|
$tagNames = array_filter(array_map('trim', explode(',', $validated['tags_input'])));
|
|
$tagIds = [];
|
|
foreach ($tagNames as $tagName) {
|
|
$tag = ArticleTag::firstOrCreate(
|
|
['name' => $tagName],
|
|
['slug' => Str::slug($tagName) ?: 'tag-'.time()]
|
|
);
|
|
$tagIds[] = $tag->id;
|
|
}
|
|
$article->tags()->sync($tagIds);
|
|
} else {
|
|
$article->tags()->sync([]);
|
|
}
|
|
|
|
AuditLog::create([
|
|
'user_id' => auth()->id(),
|
|
'action' => 'article.updated',
|
|
'description' => "更新文章:{$article->title}",
|
|
'ip_address' => request()->ip(),
|
|
]);
|
|
|
|
return redirect()
|
|
->route('admin.articles.show', $article)
|
|
->with('status', '文章已成功更新');
|
|
}
|
|
|
|
public function destroy(Article $article)
|
|
{
|
|
if (! $article->canBeEditedBy(auth()->user())) {
|
|
abort(403, '您沒有權限刪除此文章');
|
|
}
|
|
|
|
$title = $article->title;
|
|
$article->delete();
|
|
|
|
AuditLog::create([
|
|
'user_id' => auth()->id(),
|
|
'action' => 'article.deleted',
|
|
'description' => "刪除文章:{$title}",
|
|
'ip_address' => request()->ip(),
|
|
]);
|
|
|
|
return redirect()
|
|
->route('admin.articles.index')
|
|
->with('status', '文章已成功刪除');
|
|
}
|
|
|
|
public function publish(Article $article)
|
|
{
|
|
if (! auth()->user()->can('publish_articles')) {
|
|
abort(403, '您沒有權限發布文章');
|
|
}
|
|
|
|
if (! $article->canBeEditedBy(auth()->user())) {
|
|
abort(403, '您沒有權限發布此文章');
|
|
}
|
|
|
|
if ($article->isPublished()) {
|
|
return back()->with('error', '此文章已經發布');
|
|
}
|
|
|
|
$article->publish(auth()->user());
|
|
|
|
AuditLog::create([
|
|
'user_id' => auth()->id(),
|
|
'action' => 'article.published',
|
|
'description' => "發布文章:{$article->title}",
|
|
'ip_address' => request()->ip(),
|
|
]);
|
|
|
|
return back()->with('status', '文章已成功發布');
|
|
}
|
|
|
|
public function archive(Article $article)
|
|
{
|
|
if (! auth()->user()->can('publish_articles')) {
|
|
abort(403, '您沒有權限歸檔文章');
|
|
}
|
|
|
|
if (! $article->canBeEditedBy(auth()->user())) {
|
|
abort(403, '您沒有權限歸檔此文章');
|
|
}
|
|
|
|
if ($article->isArchived()) {
|
|
return back()->with('error', '此文章已經歸檔');
|
|
}
|
|
|
|
$article->archive(auth()->user());
|
|
|
|
AuditLog::create([
|
|
'user_id' => auth()->id(),
|
|
'action' => 'article.archived',
|
|
'description' => "歸檔文章:{$article->title}",
|
|
'ip_address' => request()->ip(),
|
|
]);
|
|
|
|
return back()->with('status', '文章已成功歸檔');
|
|
}
|
|
|
|
public function pin(Request $request, Article $article)
|
|
{
|
|
if (! auth()->user()->can('edit_articles')) {
|
|
abort(403, '您沒有權限置頂文章');
|
|
}
|
|
|
|
if (! $article->canBeEditedBy(auth()->user())) {
|
|
abort(403, '您沒有權限置頂此文章');
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'display_order' => 'nullable|integer',
|
|
]);
|
|
|
|
$article->pin($validated['display_order'] ?? 0, auth()->user());
|
|
|
|
AuditLog::create([
|
|
'user_id' => auth()->id(),
|
|
'action' => 'article.pinned',
|
|
'description' => "置頂文章:{$article->title}",
|
|
'ip_address' => request()->ip(),
|
|
]);
|
|
|
|
return back()->with('status', '文章已成功置頂');
|
|
}
|
|
|
|
public function unpin(Article $article)
|
|
{
|
|
if (! auth()->user()->can('edit_articles')) {
|
|
abort(403, '您沒有權限取消置頂文章');
|
|
}
|
|
|
|
if (! $article->canBeEditedBy(auth()->user())) {
|
|
abort(403, '您沒有權限取消置頂此文章');
|
|
}
|
|
|
|
$article->unpin(auth()->user());
|
|
|
|
AuditLog::create([
|
|
'user_id' => auth()->id(),
|
|
'action' => 'article.unpinned',
|
|
'description' => "取消置頂文章:{$article->title}",
|
|
'ip_address' => request()->ip(),
|
|
]);
|
|
|
|
return back()->with('status', '文章已取消置頂');
|
|
}
|
|
|
|
public function uploadAttachment(Request $request, Article $article)
|
|
{
|
|
if (! $article->canBeEditedBy(auth()->user())) {
|
|
abort(403, '您沒有權限上傳附件');
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'attachment' => 'required|file|max:20480',
|
|
'description' => 'nullable|string|max:255',
|
|
]);
|
|
|
|
$file = $request->file('attachment');
|
|
$path = $file->store('articles/attachments', 'public');
|
|
|
|
$attachment = $article->attachments()->create([
|
|
'file_path' => $path,
|
|
'original_filename' => $file->getClientOriginalName(),
|
|
'mime_type' => $file->getMimeType(),
|
|
'file_size' => $file->getSize(),
|
|
'description' => $validated['description'] ?? null,
|
|
]);
|
|
|
|
AuditLog::create([
|
|
'user_id' => auth()->id(),
|
|
'action' => 'article.attachment_uploaded',
|
|
'description' => "上傳附件至文章「{$article->title}」:{$attachment->original_filename}",
|
|
'ip_address' => request()->ip(),
|
|
]);
|
|
|
|
return back()->with('status', '附件已成功上傳');
|
|
}
|
|
|
|
public function deleteAttachment(Article $article, ArticleAttachment $attachment)
|
|
{
|
|
if (! $article->canBeEditedBy(auth()->user())) {
|
|
abort(403, '您沒有權限刪除附件');
|
|
}
|
|
|
|
if ($attachment->article_id !== $article->id) {
|
|
abort(404);
|
|
}
|
|
|
|
Storage::disk('public')->delete($attachment->file_path);
|
|
|
|
$filename = $attachment->original_filename;
|
|
$attachment->delete();
|
|
|
|
AuditLog::create([
|
|
'user_id' => auth()->id(),
|
|
'action' => 'article.attachment_deleted',
|
|
'description' => "刪除文章「{$article->title}」的附件:{$filename}",
|
|
'ip_address' => request()->ip(),
|
|
]);
|
|
|
|
return back()->with('status', '附件已成功刪除');
|
|
}
|
|
}
|