Skip to content

Instantly share code, notes, and snippets.

@1isten
Last active January 22, 2024 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1isten/2b65ebc66d9486d03940a5dce873bccc to your computer and use it in GitHub Desktop.
Save 1isten/2b65ebc66d9486d03940a5dce873bccc to your computer and use it in GitHub Desktop.
laravel route serve static
<?php
// Route::get('/', ...
// ...
Route::fallback(function () {
$static = 'static'; // static folder in project root
$static_file = request()->path() === '/' ? base_path($static) : implode(DIRECTORY_SEPARATOR, array_merge(array(base_path($static)), explode('/', request()->path())));
if (is_dir($static_file)) {
$static_file = implode(DIRECTORY_SEPARATOR, array($static_file, 'index.html'));
}
if (file_exists($static_file)) {
$mimeType = \GuzzleHttp\Psr7\MimeType::fromFilename($static_file);
return response()->file($static_file, [
// 'Cache-Control' => 'public,max-age=0,must-revalidate',
'Content-Type' => $mimeType ? $mimeType : 'application/octet-stream',
]);
}
return response([
'error' => 404,
'message' => '404 Not Found',
], 404);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment