Skip to content

Instantly share code, notes, and snippets.

@assertchris
Created January 3, 2022 08:51
Show Gist options
  • Save assertchris/39b2b772b75d6b074d83f44752d8a433 to your computer and use it in GitHub Desktop.
Save assertchris/39b2b772b75d6b074d83f44752d8a433 to your computer and use it in GitHub Desktop.
<?php
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
use Spatie\YamlFrontMatter\YamlFrontMatter;
Route::get('{slug?}', function ($slug = 'index') {
$path = resource_path('pages/' . $slug . '.md');
if (!file_exists($path)) {
abort(404);
}
$data = cache()->remember($slug . filemtime($path), 60 * 60, function() use ($path) {
$raw = file_get_contents($path);
$object = YamlFrontMatter::parse($raw);
return [
'title' => $object->matter('title'),
'body' => Str::markdown($object->body()),
];
});
return view('page', $data);
})->where('slug', '.*');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment