Læs @webhouse/cms-indhold fra en Laravel-applikation. Hjælpeklasse, routes, Blade-skabeloner.
Opsætning
Placér dit Laravel-projekt og @webhouse/cms-indhold side om side:
mit-projekt/
cms.config.ts # Indholdsmodel
content/ # JSON-dokumenter (læses af Laravel)
public/uploads/ # Mediefiler (serveres af Laravel)
app/ # Din Laravel-app
resources/views/ # Blade-skabelonerHjælpeklasse
Opret app/Services/Webhouse.php:
<?php
namespace App\Services;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Collection;
class Webhouse
{
public static function contentDir(): string
{
return base_path('content');
}
/**
* List alle publicerede dokumenter i en collection.
*/
public static function collection(string $collection, ?string $locale = null): Collection
{
$dir = self::contentDir() . '/' . $collection;
if (!File::isDirectory($dir)) {
return collect();
}
return collect(File::files($dir))
->filter(fn($f) => $f->getExtension() === 'json')
->map(fn($f) => json_decode(File::get($f->getPathname()), true))
->filter(fn($d) => ($d['status'] ?? null) === 'published')
->when($locale, fn($c) => $c->filter(fn($d) => ($d['locale'] ?? 'en') === $locale))
->sortByDesc(fn($d) => $d['data']['date'] ?? '')
->values();
}
/**
* Hent et enkelt dokument via slug.
*/
public static function document(string $collection, string $slug): ?array
{
$path = self::contentDir() . "/{$collection}/{$slug}.json";
if (!File::exists($path)) return null;
$doc = json_decode(File::get($path), true);
return ($doc['status'] ?? null) === 'published' ? $doc : null;
}
}Routes
// routes/web.php
use App\Services\Webhouse;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
$posts = Webhouse::collection('posts', 'da');
return view('home', ['posts' => $posts]);
});
Route::get('/blog/{slug}', function (string $slug) {
$post = Webhouse::document('posts', $slug);
abort_unless($post, 404);
return view('post', ['post' => $post]);
});Blade-skabelon
{{-- resources/views/post.blade.php --}}
@extends('layouts.app')
@section('content')
<article>
<h1>{{ $post['data']['title'] }}</h1>
<time>{{ $post['data']['date'] ?? '' }}</time>
<div class="prose">
{!! \Illuminate\Support\Str::markdown($post['data']['content'] ?? '') !!}
</div>
@foreach (($post['data']['tags'] ?? []) as $tag)
<a href="/tags/{{ $tag }}" class="tag">#{{ $tag }}</a>
@endforeach
</article>
@endsectionServering af uploadede medier
@webhouse/cms gemmer uploadede medier i public/uploads/. Laravel serverer allerede public/-mappen, så /uploads/mit-billede.jpg virker bare.
i18n: læs begge sprog
// Hent alle danske indlæg
$daPosts = Webhouse::collection('posts', 'da');
// Hent alle engelske indlæg
$enPosts = Webhouse::collection('posts', 'en');
// Find oversættelsen af et specifikt indlæg
$post = Webhouse::document('posts', 'hello-world');
$translationGroup = $post['translationGroup'] ?? null;
$translation = Webhouse::collection('posts')
->firstWhere(fn($d) => ($d['translationGroup'] ?? null) === $translationGroup && ($d['locale'] ?? null) !== $post['locale']);Caching
I produktion, cache parsed JSON:
use Illuminate\Support\Facades\Cache;
public static function collection(string $collection, ?string $locale = null): Collection
{
return Cache::remember("webhouse:{$collection}:{$locale}", 60, function () use ($collection, $locale) {
// ... samme logik som før
});
}Ryd cachen når indhold ændres — enten via en fil-watcher eller et CMS admin webhook.
Næste skridt
- Se Laravel-eksemplet
- Læs om i18n for flersprogsmønstre
- Læs om Framework-agnostisk arkitektur