How to serve static HTML with Laravel
Step 1
- Create a Laravel application.
laravel new static-site
Step 2
- Create a
pagesfolder in theresourcesdirectory.
Step 3
-
Create HTML files in the newly created
pagesfolder. -
You can get the HTML file with its content using PHP’s inbuilt
file_get_contents(). -
Pass the path to the
file_get_contents()function to get the file. This can then be returned like so:
return file_get_contents(__DIR__ . '/../resources/pages/first-post.html');
This example assumes you’re working in your
routes/web.phpfile. The path would be different should you be working in a controller.
Step 4
- This HTML content can also be passed in as a variable to another view.
Route::get('/', function () {
return view('welcome')->with('post', file_get_contents(__DIR__ . '/../resources/pages/first-post.html'));
});
- The
$postvariable can then be printed unescaped in the view, like this:
{!! $post !!}}