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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46050332/
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 16:36:20  来源:igfitidea点击:

Assets in the public directory not showing in Laravel 5

phpcsslaravelassets

提问by murume

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

我在 Laravel 5.4 和 Laravel 5.5的/public目录中遇到资产问题。

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

在全新安装的 Laravel 中,如果URL 中没有/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 /publicso that I end up with:

当我运行时php artisan make:auth,创建的视图应该是完美的,对吧?好吧,我得到的是一个无样式的页面,只有当我使用 DOM 检查器添加/public时它才会看起来很漂亮,这样我最终会得到:

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

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

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

回答by Sletheren

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.

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

Note:

笔记:

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

如果这不起作用,那么您很可能需要设置一个指向您/public文件夹的虚拟主机。您可以在 Apache 上的 vHosts 文件中执行此操作。

Note2:

笔记2:

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.

如果您在每个示例上使用共享主机,或者由于某种原因无法修改 vHost 文件,这里是您的解决方案。

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 将其重命名,在其中,您必须放置公用文件夹的内容。

/public_folder
/your_project_folder
  app/
  databases/
  ...

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

其次,在 public 文件夹中,您会找到一个名为的文件,index.php您必须对其进行如下修改:

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.

这意味着您正在从我们上面创建的公共文件夹中引用项目文件夹。

Note3:

注3:

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

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

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

Hope it makes sense

希望这是有道理的

回答by murume

This is what worked for me: To urlin config\app.phpI entered the full URL to the publicdirectory like this:

这对我有用:urlconfig\app.php我输入public目录的完整 URL 时,如下所示:

'url' => env('APP_URL', 'http://localhost/l5.5/public'),

I also entered the same URL to APP_URLin the .envfile because that is what I'm using for configuration. Apparently asset()and similar functions append to the URL in the configuration file when constructing the file URL

我也进入了相同的URLAPP_URL.env的文件,因为这是我使用的是什么配置。显然asset(),类似的函数在构造文件 URL 时附加到配置文件中的 URL

UPDATE:The problem with doing it this way is that I'm now having to edit Illuminate\Foundation\helpers.phpwhich I believe is a bad idea. I believe core files should not be touched. Now instead of

更新:这样做的问题是我现在不得不编辑Illuminate\Foundation\helpers.php,我认为这是一个坏主意。我认为不应该触及核心文件。现在而不是

function asset($path, $secure = null)
{
    return app('url')->asset($path, $secure);
}

I am using

我在用

function asset($path)
{
    return config('app.url'). '/' .$path;
}

May someone tell me the proper way of doing this?

有人可以告诉我这样做的正确方法吗?

回答by mytimenow

<link href="{{ asset('css/app.css') }}" rel="stylesheet">

This works just fine. When Laravel runs on the server the main index file and all assets are in the “public directory”. So the css asset files would start with domain name + css/file.css not domain/public/css/file.css.

这工作得很好。当 Laravel 在服务器上运行时,主索引文件和所有资产都在“公共目录”中。因此 css 资产文件将以域名 + css/file.css 开头,而不是 domain/public/css/file.css。

回答by Hasnat Safder

In root folder rename server.phpto index.phpand copy .htaccessfile from public folder to root folder. Also share your directory structure to show where your assets are places.

在根文件夹中,将server.php重命名为index.php并将.htaccess文件从公共文件夹复制到根文件夹。还可以共享您的目录结构以显示您的资产所在的位置。

回答by Farshad

just do this

就这样做

<link href="{{ asset('css/app.css') }}" rel="stylesheet">