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

130
app/Models/Page.php Normal file
View File

@@ -0,0 +1,130 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
class Page extends Model
{
use HasFactory, SoftDeletes;
const STATUS_DRAFT = 'draft';
const STATUS_PUBLISHED = 'published';
protected $fillable = [
'title',
'slug',
'content',
'template',
'custom_fields',
'status',
'meta_description',
'meta_keywords',
'parent_id',
'sort_order',
'published_at',
'created_by_user_id',
'last_updated_by_user_id',
];
protected $casts = [
'custom_fields' => 'array',
'meta_keywords' => 'array',
'sort_order' => 'integer',
'published_at' => 'datetime',
];
protected static function boot()
{
parent::boot();
static::creating(function (Page $page) {
if (empty($page->slug)) {
$slug = Str::slug($page->title);
if (empty($slug)) {
$slug = 'page-'.time();
}
$originalSlug = $slug;
$count = 1;
while (static::withTrashed()->where('slug', $slug)->exists()) {
$slug = $originalSlug.'-'.$count++;
}
$page->slug = $slug;
}
});
}
// ==================== Relationships ====================
public function parent()
{
return $this->belongsTo(Page::class, 'parent_id');
}
public function children()
{
return $this->hasMany(Page::class, 'parent_id')->orderBy('sort_order');
}
public function creator()
{
return $this->belongsTo(User::class, 'created_by_user_id');
}
public function lastUpdatedBy()
{
return $this->belongsTo(User::class, 'last_updated_by_user_id');
}
// ==================== Query Scopes ====================
public function scopePublished(Builder $query): Builder
{
return $query->where('status', self::STATUS_PUBLISHED);
}
public function scopeTopLevel(Builder $query): Builder
{
return $query->whereNull('parent_id');
}
// ==================== Helper Methods ====================
public function isDraft(): bool
{
return $this->status === self::STATUS_DRAFT;
}
public function isPublished(): bool
{
return $this->status === self::STATUS_PUBLISHED;
}
public function publish(?User $user = null): void
{
$updates = [
'status' => self::STATUS_PUBLISHED,
'published_at' => $this->published_at ?? now(),
];
if ($user) {
$updates['last_updated_by_user_id'] = $user->id;
}
$this->update($updates);
}
public function getStatusLabel(): string
{
return match ($this->status) {
self::STATUS_DRAFT => '草稿',
self::STATUS_PUBLISHED => '已發布',
default => '未知',
};
}
}