Created
January 3, 2022 08:51
-
-
Save assertchris/39b2b772b75d6b074d83f44752d8a433 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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