Load an HTML file directly from public folder as View in Laravel

23,779

Solution 1

Just place the wedding folder directly inside the public folder:

mv wedding/ /path/to/laravel/public

Then visit your site URL with a wedding suffix:

http://my-site.com/wedding

This will load the index.html from inside the wedding folder.

This works via Nginx's try_files directive in your /etc/nginx/sites-enabled/my-site config file:

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

This instructs Nginx to first search for an actual file corresponding to the URL (for example, /css/app.css, /wedding/index.html, etc). If it does not find a matching file (e.g. it would normally return a 404 not found), then it should instead pass the query string as an argument to the index.php script.

Solution 2

Load static files from controller using File Facade

use File;

return File::get(public_path() . '/to new folder name/index.html');

Solution 3

One solution is to rename your wedding invitation's index.html to index.php and place it within your resources/views folder, so that it becomes a Laravel template.

cd path/to/laravel
mkdir resources/views/wedding
mv public/wedding/index.html resources/views/wedding/index.php

Then you can call it from your controller as you wish:

public function index()
{
    return view('wedding.index');
}

Of course with this method you'll have to ensure any CSS/JavaScript/image urls are properly mapped to their respective locations within the public/ folder.

Share:
23,779
code-8
Author by

code-8

I'm B, I'm a cyb3r-full-stack-web-developer. I love anything that is related to web design/development/security, and I've been in the field for about ~9+ years. I do freelance on the side, if you need a web project done, message me. ;)

Updated on July 15, 2022

Comments

  • code-8
    code-8 almost 2 years

    I'm using Laravel. I'm trying to put together my own wedding invitation. I have a folder in my public folder, will styles, and scripts for it.

    I'm wondering if I can point to that folder, instead of copy-and-paste everything into a new blade file.


    Route

    Route::get('/wedding', 'WeddingController@index');


    WeddingController

    <?php namespace App\Http\Controllers;
    
    class WeddingController extends Controller {
    
    
        public function index()
        {
            return view('wedding.index');
        }
    
    }
    

    Question

    I'm wondering if I can point my index function to load the index.html from one of my folder in my /public folder.

    Do we have to load the .blade file from the app/resources/views directory ?

    Any helps / suggestions on this will be much appreciated.