Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

laravel8
webdevelopment
communitycreator

How to serve static HTML with Laravel

Zubair Idris Aweda

Step 1

  • Create a Laravel application.
laravel new static-site

Step 2

  • Create a pages folder in the resources directory.

Step 3

  • Create HTML files in the newly created pages folder.

  • 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.php file. 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 $post variable can then be printed unescaped in the view, like this:
{!! $post !!}}

RELATED TAGS

laravel8
webdevelopment
communitycreator
RELATED COURSES

View all Courses

Keep Exploring