9

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.

4
  • Is there a domain specific reason you cannot use blade templates to serve your angular application? Commented Aug 21, 2014 at 4:55
  • @watcher I am using other languages besides PHP. A single client directory served by many frameworks using various languages. Commented Aug 21, 2014 at 4:58
  • what is your project directory structure? You could be better off serving your angular application by default rather than laravel and instead trying to bootstrap laravel only for the requests that it is meant to handle rather than the other way around (if that's what you're trying and I'm understanding you correctly) Commented Aug 21, 2014 at 5:06
  • @watcher The approach you are suggesting requires enabling cross origin request policy which I don't know how to do, since I will need to start serving AngularJS via python -m SimpleHTTPServer for example and Laravel server on another port. Commented Aug 21, 2014 at 5:26

3 Answers 3

12

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hope the TS could mark this as the accepted answer. Ran into this issue as well and added the addExtension. It now accepts either php or html file.
Where are you putting the code View::addExtension('html', 'php')?
3

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).

Comments

3

If you want to serve for local development using php artisan serve, change following lines in "server.php"

    if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
        return false;
    }

    require_once __DIR__.'/public/index.php';

to

    if ($uri === '/') {
        $uri = "/index.html";
        return false;
    }
    if (file_exists(__DIR__.'/public'.$uri)) {
        return false;
    }

    require_once __DIR__.'/public/api/index.php';

Now you can serve "index.html" as default document.

Only works if angular build output is in public folder with laravel api (index.php and .htaccess) in subfolder.

Hope it can help someone!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.