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>
113 lines
3.5 KiB
PHP
113 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\ArticleCategory;
|
|
use App\Models\AuditLog;
|
|
use Illuminate\Http\Request;
|
|
|
|
class ArticleCategoryController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware('can:view_articles');
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$categories = ArticleCategory::withCount('articles')
|
|
->orderBy('sort_order')
|
|
->get();
|
|
|
|
return view('admin.article-categories.index', compact('categories'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
return view('admin.article-categories.create');
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'slug' => 'nullable|string|max:255|unique:article_categories,slug',
|
|
'description' => 'nullable|string|max:1000',
|
|
'sort_order' => 'nullable|integer',
|
|
]);
|
|
|
|
$category = ArticleCategory::create([
|
|
'name' => $validated['name'],
|
|
'slug' => $validated['slug'] ?? null,
|
|
'description' => $validated['description'] ?? null,
|
|
'sort_order' => $validated['sort_order'] ?? 0,
|
|
]);
|
|
|
|
AuditLog::create([
|
|
'user_id' => auth()->id(),
|
|
'action' => 'article_category.created',
|
|
'description' => "建立文章分類:{$category->name}",
|
|
'ip_address' => request()->ip(),
|
|
]);
|
|
|
|
return redirect()
|
|
->route('admin.article-categories.index')
|
|
->with('status', '分類已成功建立');
|
|
}
|
|
|
|
public function edit(ArticleCategory $articleCategory)
|
|
{
|
|
return view('admin.article-categories.edit', ['category' => $articleCategory]);
|
|
}
|
|
|
|
public function update(Request $request, ArticleCategory $articleCategory)
|
|
{
|
|
$validated = $request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'slug' => 'nullable|string|max:255|unique:article_categories,slug,'.$articleCategory->id,
|
|
'description' => 'nullable|string|max:1000',
|
|
'sort_order' => 'nullable|integer',
|
|
]);
|
|
|
|
$articleCategory->update([
|
|
'name' => $validated['name'],
|
|
'slug' => $validated['slug'] ?? $articleCategory->slug,
|
|
'description' => $validated['description'] ?? null,
|
|
'sort_order' => $validated['sort_order'] ?? 0,
|
|
]);
|
|
|
|
AuditLog::create([
|
|
'user_id' => auth()->id(),
|
|
'action' => 'article_category.updated',
|
|
'description' => "更新文章分類:{$articleCategory->name}",
|
|
'ip_address' => request()->ip(),
|
|
]);
|
|
|
|
return redirect()
|
|
->route('admin.article-categories.index')
|
|
->with('status', '分類已成功更新');
|
|
}
|
|
|
|
public function destroy(ArticleCategory $articleCategory)
|
|
{
|
|
if ($articleCategory->articles()->count() > 0) {
|
|
return back()->with('error', '此分類下仍有文章,無法刪除');
|
|
}
|
|
|
|
$name = $articleCategory->name;
|
|
$articleCategory->delete();
|
|
|
|
AuditLog::create([
|
|
'user_id' => auth()->id(),
|
|
'action' => 'article_category.deleted',
|
|
'description' => "刪除文章分類:{$name}",
|
|
'ip_address' => request()->ip(),
|
|
]);
|
|
|
|
return redirect()
|
|
->route('admin.article-categories.index')
|
|
->with('status', '分類已成功刪除');
|
|
}
|
|
}
|