php Laravel 5 资产链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32705709/
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
Laravel 5 assets links
提问by WebArtisan
I try include my css or js files to project using:
我尝试使用以下方法将我的 css 或 js 文件包含到项目中:
{{ URL::asset('path to css') }}
or
或者
{!! Html::style("path to css") !!}
or similar.
或类似。
But i always got link from publicdirectory. like so: http://localhost/projectroot/public/assets/style.css
但我总是从公共目录获得链接。像这样:http://localhost/projectroot/public/assets/style.css
Why that happens and how can i solve this?
为什么会发生这种情况,我该如何解决?
Seems to i should move my assets into public ?
似乎我应该将我的资产公之于众?
回答by Joseph Dan Alinsug
use the helper asset('path/to/css')
使用帮手 asset('path/to/css')
e.g.
例如
<link rel="stylesheet" type="text/css" href="{{ asset('assets/style.css') }}">
assuming that your assets/style.css
is under public
假设你assets/style.css
是公开的
回答by Deepak Soni
In the simplest form, assets means JavaScript, CSS, and images which lie directly under the public directoryand are publicly accessible using a URL.
在最简单的形式中,资产意味着 JavaScript、CSS 和图像,它们直接位于public 目录下并且可以使用 URL 公开访问。
Laravel provides a helper function, asset()
, which generates a URL for your assets. You can use this in blade syntax, e.g.
Laravel 提供了一个辅助函数 ,asset()
它为您的资产生成一个 URL。您可以在刀片语法中使用它,例如
<script type="text/javascript" src="{ { asset('js/jquery.js') } }"></script>
回答by bernie
That's perfectly normal. Your assets should be in the public
folder. If you look at the .htaccess
file in there, you'll see this:
这是完全正常的。您的资产应该在public
文件夹中。如果你查看.htaccess
那里的文件,你会看到:
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
This ensures that files located under /public
are served directly without involving Laravel while all other urls go through index.php
.
这确保了在/public
不涉及 Laravel 的情况下直接提供位于index.php
.
Also, your server configuration should make sure only the /public
folder is URL-mapped since sensitive info can be leaked via parent paths (e.g. config files) otherwise.
此外,您的服务器配置应确保只有/public
文件夹是 URL 映射的,否则敏感信息可能会通过父路径(例如配置文件)泄露。