如何使用 Laravel 提供 index.html?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25418156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:58:27  来源:igfitidea点击:

How to serve index.html with Laravel?

phplaravel

提问by Sahat Yalkabov

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

我很难理解有人如何index.html使用 Laravel加载文件。它似乎只适用于 index.php,这不是我想要的。

Here is a problem:

这里有一个问题:

I have two folders clientand server. My serverfolder contains the entire Laravel app. My clientdirectory contains AngularJS app. (I can't use publicdirectory)

我有两个文件夹clientserver。我的服务器文件夹包含整个 Laravel 应用程序。我的客户端目录包含 AngularJS 应用程序。(我不能使用公共目录)

I want Laravel to serve index.htmlalong with all static files from the clientdirectory.

我希望 Laravelindex.html客户端目录中的所有静态文件一起提供服务。

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

更改'public' => __DIR__.'/../public'确实有些工作,但是它破坏了我的所有路由,即无论我输入什么 URLindex.html都会加载损坏的静态资产。例如/api/users不再返回 JSON 对象,而是返回相同的index.html.

回答by user2600285

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

我不确定您的文件结构发生了什么,但是,要通过 php 引擎加载 html 文件,您可以使用:

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

Then,

然后,

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

should return index.html.

应该返回 index.html。

回答by Jeff Lambert

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:

在 Laravel 中使用刀片模板。您的机箱非常独特,因此它不会是“普通”刀片模板。它看起来像这样:

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

If you can instead use an index.phpfile inside of your /clientdirectory it could also look like this:

如果您可以改用目录中的index.php文件,/client它也可能如下所示:

<?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.phpfile with the normal View::make('angular')->with('var', $var)and they will automatically be in scope.

我只是指出这一点,因为如果您需要或希望,您可以在文件中使用其他 PHP 语句。您还可以潜在地将数据从您的控制器一直向下传递到index.php正常的文件中,View::make('angular')->with('var', $var)它们将自动在范围内。

This seems a little hacky, and it is, but should keep you from having to modify anything else. Of course, your index.htmlfile 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).

这看起来有点老套,确实如此,但应该可以让您不必修改任何其他内容。当然,您的index.html文件应该包含 angular 应用程序的整个标记。另一个好处是,由于您几乎仍然完全包含在 Laravel 中,如果不需要(无论出于何种原因),您可能会将内容“向上”移出您的 angular 应用程序并返回到框架中。

回答by Saravana Kumar

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

如果您想使用 为本地开发服务php artisan serve,请更改“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.

现在您可以将“index.html”作为默认文档。

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

仅当 angular 构建输出位于公共文件夹中且子文件夹中包含 laravel api(index.php 和 .htaccess)时才有效。

Hope it can help someone!

希望它可以帮助某人!