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', '頁面已成功發布');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user