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:
112
app/Http/Controllers/Admin/ArticleCategoryController.php
Normal file
112
app/Http/Controllers/Admin/ArticleCategoryController.php
Normal file
@@ -0,0 +1,112 @@
|
||||
<?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', '分類已成功刪除');
|
||||
}
|
||||
}
|
||||
449
app/Http/Controllers/Admin/ArticleController.php
Normal file
449
app/Http/Controllers/Admin/ArticleController.php
Normal file
@@ -0,0 +1,449 @@
|
||||
<?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', '附件已成功刪除');
|
||||
}
|
||||
}
|
||||
101
app/Http/Controllers/Admin/ArticleTagController.php
Normal file
101
app/Http/Controllers/Admin/ArticleTagController.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\ArticleTag;
|
||||
use App\Models\AuditLog;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ArticleTagController extends Controller
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('can:view_articles');
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
$tags = ArticleTag::withCount('articles')
|
||||
->orderBy('name')
|
||||
->get();
|
||||
|
||||
return view('admin.article-tags.index', compact('tags'));
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
return view('admin.article-tags.create');
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
'slug' => 'nullable|string|max:255|unique:article_tags,slug',
|
||||
]);
|
||||
|
||||
$tag = ArticleTag::create([
|
||||
'name' => $validated['name'],
|
||||
'slug' => $validated['slug'] ?? null,
|
||||
]);
|
||||
|
||||
AuditLog::create([
|
||||
'user_id' => auth()->id(),
|
||||
'action' => 'article_tag.created',
|
||||
'description' => "建立文章標籤:{$tag->name}",
|
||||
'ip_address' => request()->ip(),
|
||||
]);
|
||||
|
||||
return redirect()
|
||||
->route('admin.article-tags.index')
|
||||
->with('status', '標籤已成功建立');
|
||||
}
|
||||
|
||||
public function edit(ArticleTag $articleTag)
|
||||
{
|
||||
return view('admin.article-tags.edit', ['tag' => $articleTag]);
|
||||
}
|
||||
|
||||
public function update(Request $request, ArticleTag $articleTag)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
'slug' => 'nullable|string|max:255|unique:article_tags,slug,'.$articleTag->id,
|
||||
]);
|
||||
|
||||
$articleTag->update([
|
||||
'name' => $validated['name'],
|
||||
'slug' => $validated['slug'] ?? $articleTag->slug,
|
||||
]);
|
||||
|
||||
AuditLog::create([
|
||||
'user_id' => auth()->id(),
|
||||
'action' => 'article_tag.updated',
|
||||
'description' => "更新文章標籤:{$articleTag->name}",
|
||||
'ip_address' => request()->ip(),
|
||||
]);
|
||||
|
||||
return redirect()
|
||||
->route('admin.article-tags.index')
|
||||
->with('status', '標籤已成功更新');
|
||||
}
|
||||
|
||||
public function destroy(ArticleTag $articleTag)
|
||||
{
|
||||
$name = $articleTag->name;
|
||||
$articleTag->articles()->detach();
|
||||
$articleTag->delete();
|
||||
|
||||
AuditLog::create([
|
||||
'user_id' => auth()->id(),
|
||||
'action' => 'article_tag.deleted',
|
||||
'description' => "刪除文章標籤:{$name}",
|
||||
'ip_address' => request()->ip(),
|
||||
]);
|
||||
|
||||
return redirect()
|
||||
->route('admin.article-tags.index')
|
||||
->with('status', '標籤已成功刪除');
|
||||
}
|
||||
}
|
||||
181
app/Http/Controllers/Admin/PageController.php
Normal file
181
app/Http/Controllers/Admin/PageController.php
Normal 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', '頁面已成功發布');
|
||||
}
|
||||
}
|
||||
94
app/Http/Controllers/Api/ArticleController.php
Normal file
94
app/Http/Controllers/Api/ArticleController.php
Normal 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
|
||||
);
|
||||
}
|
||||
}
|
||||
81
app/Http/Controllers/Api/HomepageController.php
Normal file
81
app/Http/Controllers/Api/HomepageController.php
Normal 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),
|
||||
]);
|
||||
}
|
||||
}
|
||||
22
app/Http/Controllers/Api/PageController.php
Normal file
22
app/Http/Controllers/Api/PageController.php
Normal 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);
|
||||
}
|
||||
}
|
||||
34
app/Http/Resources/ArticleCollectionResource.php
Normal file
34
app/Http/Resources/ArticleCollectionResource.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class ArticleCollectionResource extends JsonResource
|
||||
{
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'title' => $this->title,
|
||||
'slug' => $this->slug,
|
||||
'summary' => $this->summary,
|
||||
'excerpt' => $this->getExcerpt(200),
|
||||
'content_type' => $this->content_type,
|
||||
'content_type_label' => $this->getContentTypeLabel(),
|
||||
'featured_image_url' => $this->featured_image_url,
|
||||
'featured_image_alt' => $this->featured_image_alt,
|
||||
'author_name' => $this->author_name,
|
||||
'is_pinned' => $this->is_pinned,
|
||||
'published_at' => $this->published_at?->toIso8601String(),
|
||||
'categories' => CategoryResource::collection($this->whenLoaded('categories')),
|
||||
'tags' => $this->whenLoaded('tags', function () {
|
||||
return $this->tags->map(fn ($tag) => [
|
||||
'name' => $tag->name,
|
||||
'slug' => $tag->slug,
|
||||
]);
|
||||
}),
|
||||
];
|
||||
}
|
||||
}
|
||||
47
app/Http/Resources/ArticleResource.php
Normal file
47
app/Http/Resources/ArticleResource.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class ArticleResource extends JsonResource
|
||||
{
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'title' => $this->title,
|
||||
'slug' => $this->slug,
|
||||
'summary' => $this->summary,
|
||||
'content' => $this->content,
|
||||
'content_type' => $this->content_type,
|
||||
'content_type_label' => $this->getContentTypeLabel(),
|
||||
'featured_image_url' => $this->featured_image_url,
|
||||
'featured_image_alt' => $this->featured_image_alt,
|
||||
'author_name' => $this->author_name,
|
||||
'meta_description' => $this->meta_description,
|
||||
'meta_keywords' => $this->meta_keywords,
|
||||
'is_pinned' => $this->is_pinned,
|
||||
'view_count' => $this->view_count,
|
||||
'published_at' => $this->published_at?->toIso8601String(),
|
||||
'categories' => CategoryResource::collection($this->whenLoaded('categories')),
|
||||
'tags' => $this->whenLoaded('tags', function () {
|
||||
return $this->tags->map(fn ($tag) => [
|
||||
'id' => $tag->id,
|
||||
'name' => $tag->name,
|
||||
'slug' => $tag->slug,
|
||||
]);
|
||||
}),
|
||||
'attachments' => $this->whenLoaded('attachments', function () {
|
||||
return $this->attachments->map(fn ($att) => [
|
||||
'id' => $att->id,
|
||||
'original_filename' => $att->original_filename,
|
||||
'mime_type' => $att->mime_type,
|
||||
'file_size' => $att->file_size,
|
||||
'description' => $att->description,
|
||||
]);
|
||||
}),
|
||||
];
|
||||
}
|
||||
}
|
||||
20
app/Http/Resources/CategoryResource.php
Normal file
20
app/Http/Resources/CategoryResource.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class CategoryResource extends JsonResource
|
||||
{
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->name,
|
||||
'slug' => $this->slug,
|
||||
'description' => $this->description,
|
||||
'articles_count' => $this->whenCounted('articles'),
|
||||
];
|
||||
}
|
||||
}
|
||||
25
app/Http/Resources/PageResource.php
Normal file
25
app/Http/Resources/PageResource.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class PageResource extends JsonResource
|
||||
{
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'title' => $this->title,
|
||||
'slug' => $this->slug,
|
||||
'content' => $this->content,
|
||||
'template' => $this->template,
|
||||
'custom_fields' => $this->custom_fields,
|
||||
'meta_description' => $this->meta_description,
|
||||
'meta_keywords' => $this->meta_keywords,
|
||||
'published_at' => $this->published_at?->toIso8601String(),
|
||||
'children' => PageResource::collection($this->whenLoaded('children')),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user