Laravel 5中未显示公共目录中的资产

问题描述:

我在Laravel 5.4和Laravel 5.5的/public 目录中的资产存在问题.

I am having issues with assets in the /public directory in Laravel 5.4 and Laravel 5.5.

在全新安装的Laravel上,如果没有URL中的/public ,则公共目录中的任何内容都将无法正常工作.

On a fresh install of Laravel, nothing in the public directory will work without /public in the URL.

当我运行php artisan make:auth时,所创建的视图应该是完美的,对吗?好吧,我得到的是一个未样式化的页面,该页面仅在使用DOM检查器添加/public 时看起来很漂亮,最终得到:

When I run php artisan make:auth, the views that are created are supposed to be perfect, right? Well, what I get is an unstyled page which only looks pretty when I use the DOM inspector to add /public so that I end up with:

<link href="http://localhost/l5.4/public/css/app.css" rel="stylesheet">

我在哪里可以更改设置,这样就不必在每次要链接到资产时都在视图中添加/public ?

Where can I change the setting so that I don't have to add /public in the views each time I want to link to an asset?

您不必手动执行此操作,laravel附带了一个功能:{{asset('css/app.css')}}可直接访问公用文件夹.

You don't have to do that manually there is a function shipped with laravel: {{asset('css/app.css')}} this access directly the public folder.

注意:

如果这不起作用,那么您可能需要设置一个指向您的/public文件夹的virtualHost. 您可以在Apache上的vHosts文件中做到这一点.

If that doesn't work then you're high probably need to set up a virtualHost that points on you /public folder. You can do that in your vHosts file on Apache.

注2:

如果每个示例都位于共享主机上,或者由于某种原因而无法修改vHost文件,那么这就是您所拥有的解决方案.

If you're on a shared hosting per example, or you can't modify your vHost file for a reason or an other, here is the solution you've got.

首先,必须将公用文件夹放在项目文件夹上方,并按示例public_folder重命名,在其中,必须将公用文件夹的内容放入

First, you have to put your public folder above the project folder and rename it per example public_folder, inside it, you'll have to put the content of the public folder.

/public_folder
/your_project_folder
  app/
  databases/
  ...

第二,在公用文件夹中,您将找到一个名为index.php的文件,您必须对其进行如下修改:

Second, inside the public folder you will find a file called index.php you have to modify it as follows:

require __DIR__.'/../your_project_folder/bootstrap/autoload.php';
$app = require_once __DIR__.'/../your_project_folder/bootstrap/app.php';

这意味着您要引用我们上面创建的公用文件夹中的项目文件夹.

That means you're referencing the project folder from the public folder we created above.

注意3:

要告诉laravel要从我们公用文件夹的新位置引用资产功能,可以在$app=require_once...之后将其添加到index.php中:

To tell laravel that the asset function going to be referenced from the new location of our public folder, you can add this to your index.php, after $app=require_once... :

$app->bind('path.public', function() {
    return __DIR__;
});

希望有道理