Why are you doing this in a middleware? If you have some sort of CMS that has pages with custom paths, just create a “catch all” route that looks up the page based on the path, and then renders that page in a template:
Route::get('/{path}', DynamicPageController::class)->where('path', '.*');
class DynamicPageController extends Controller
{
public function __invoke(string $path)
{
// Look up page based on $path...
$page = someMethodToFindPage($path);
// If there is no match, return a 404 Not Found error
abort_unless($page, 404);
// Render page model in a Blade template
return view('page.show', compact('page'));
}
}