简体   繁体   English

如何使用 Laravel 提供 index.html?

[英]How to serve index.html with Laravel?

I am struggling to understand how can someone load index.html file with Laravel.我正在努力理解如何使用 Laravel 加载index.html文件。 It seems to work only with index.php which is not something I want.它似乎只适用于 index.php,这不是我想要的。

Here is a problem:这里有一个问题:

I have two folders client and server .我有两个文件夹clientserver My server folder contains the entire Laravel app.我的服务器文件夹包含整个 Laravel 应用程序。 My client directory contains AngularJS app.我的客户端目录包含 AngularJS 应用程序。 (I can't use public directory) (我不能使用公共目录)

I want Laravel to serve index.html along with all static files from the client directory.我希望 Laravel 为index.html以及来自客户端目录的所有静态文件提供服务。

Changing 'public' => __DIR__.'/../public' did somewhat work, but then it breaks all my routes, ie no matter what I type for URL index.html gets loaded with broken static assets.更改'public' => __DIR__.'/../public'确实有些作用,但是它破坏了我的所有路由,即无论我输入什么 URL index.html都会加载损坏的静态资产。 For example /api/users does not return a JSON object anymore but the same index.html .例如/api/users不再返回 JSON 对象,而是返回相同的index.html

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。

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.在 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:如果您可以改用/client目录中的index.php文件,它也可能如下所示:

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

This seems a little hacky, and it is, but should keep you from having to modify anything else.这似乎有点hacky,确实如此,但应该使您不必修改任何其他内容。 Of course, your index.html file should include the entire markup for the angular application.当然,您的index.html文件应该包含 angular 应用程序的整个标记。 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).另一个好处是,由于您几乎仍然完全包含在 Laravel 中,如果不需要(无论出于何种原因),您可能会将内容“向上”移出您的 angular 应用程序并返回到框架中。

If you want to serve for local development using php artisan serve , change following lines in "server.php"如果您想使用php artisan serve serve 为本地开发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!希望它可以帮助某人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM