Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve index.html with Laravel?

Tags:

php

laravel

I am struggling to understand how can someone load index.html file with Laravel. It seems to work only with index.php which is not something I want.

Here is a problem:

I have two folders client and server. My server folder contains the entire Laravel app. My client directory contains AngularJS app. (I can't use public directory)

I want Laravel to serve index.html along with all static files from the client directory.

Changing 'public' => __DIR__.'/../public' did somewhat work, but then it breaks all my routes, i.e. no matter what I type for URL index.html gets loaded with broken static assets. For example /api/users does not return a JSON object anymore but the same index.html.

like image 540
Sahat Yalkabov Avatar asked Aug 21 '14 04:08

Sahat Yalkabov


People also ask

Where should I put my index HTML file?

Using your text editor, create a new file called index. html and save it just inside your test-site folder.


2 Answers

I am not sure what's going on with your file structure, however, to load html files through the php engine you can use:

View::addExtension('html', 'php');

Then,

Route::get('/', function()
{
    return View::make('index');
});

should return index.html.

like image 156
user2600285 Avatar answered Oct 14 '22 08:10

user2600285


I believe I understand you now. Here's a potential solution, assuming a directory structure something along the lines of this:

/
    /client
        index.html 
    /server
        /app
        /bootstrap
        /public
        /vendor
        ...

Use a blade template within Laravel. Your case is pretty unique, so its not going to be a 'normal' blade template. It can look like this:

{{ file_get_contents(base_path() . '../client/index.html') }}

If you can instead use an index.php file inside of your /client directory it could also look like this:

<?php include base_path() . '../client/index.php'; ?>

I'm only pointing this out because you could then use other PHP statements inside of the file should you need or desire to. You could also potentially pass data from your controller all the way down and into this index.php file with the normal View::make('angular')->with('var', $var) and they will automatically be in scope.

This seems a little hacky, and it is, but should keep you from having to modify anything else. Of course, your index.html file should include the entire markup for the angular application. Another benefit is that, since you're pretty much still contained completely within Laravel, you could potentially move stuff 'up' and out of your angular application and back into the framework should it not be necessary there (for whatever reason).

like image 38
Jeff Lambert Avatar answered Oct 14 '22 07:10

Jeff Lambert