code-8
code-8

Reputation: 58642

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

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.

Upvotes: 17

Views: 33335

Answers (3)

Ankit Jain
Ankit Jain

Reputation: 1036

Load static files from controller using File Facade

use File;

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

Upvotes: 8

Ben Claar
Ben Claar

Reputation: 3415

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.

Upvotes: 11

Ben Claar
Ben Claar

Reputation: 3415

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.

Upvotes: 1

Related Questions