Skip to content

Instantly share code, notes, and snippets.

@captenmasin
Created February 3, 2024 23:14
Show Gist options
  • Save captenmasin/f81cb1a7354e7cf0ade6e82541718278 to your computer and use it in GitHub Desktop.
Save captenmasin/f81cb1a7354e7cf0ade6e82541718278 to your computer and use it in GitHub Desktop.
Nice simple content management for "generic" pages
<?php
namespace App\Http\Controllers;
use Cache;
use Illuminate\Support\Str;
use Symfony\Component\Yaml\Yaml;
class GenericPageController
{
public function __invoke()
{
$file = request()->path();
$filePath = resource_path().'/content/'.$file.'.yaml';
if (!file_exists($filePath)) {
abort(404);
}
[$parsedFile, $content] = Cache::rememberForever('page:'.$file, function () use ($filePath) {
$parsedFile = Yaml::parse(file_get_contents($filePath));
$content = Str::markdown($parsedFile['content']);
return [$parsedFile, $content];
});
return view('generic', [
'title' => $parsedFile['title'] ?? '',
'description' => $parsedFile['description'] ?? null,
'date' => $parsedFile['date'] ?? null,
'content' => $content ?? '',
]);
}
}
title: "Terms & Conditions"
date: "2024-02-03"
content: |
# Lorem ipsum!
This is a **Markdown** example.
## Here's a list:
- Item 1
- Item 2
- Item 3
//
Route::get('terms', GenericPageController::class);
Route::get('privacy', GenericPageController::class);
Route::get('etc-etc', GenericPageController::class);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment