Laravel 5.7 - CSS 和 JS 不使用 URL::asset 加载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53746423/
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.7 - CSS and JS not loading using URL::asset
提问by Ankit
I am new in laravel. I setup laravel 5.7 on my local system and put js and css into public folder but when hitting my site url on browser, site is not loading any css and js.
我是laravel的新手。我在本地系统上设置了 laravel 5.7 并将 js 和 css 放入公共文件夹,但是当在浏览器上点击我的站点 url 时,站点没有加载任何 css 和 js。
Site structure :
网站结构:
app
bootstrap
config
database
public
-css
-js
-images
resources
-views
-includes
-layouts
-pages
routes
tests
vendor
index.php
server.php
.env
Note : I have cut .htaccess and index.php files and put these on root to run site without public in path.
注意:我已经剪切了 .htaccess 和 index.php 文件并将它们放在 root 上以在没有 public 路径的情况下运行站点。
Here is how I am calling url : CSS :
这是我如何调用 url : CSS :
{{ URL::asset('css/style.css') }}
JS :
JS:
{{ URL::asset('js/query-1.11.1.min.js') }}
When seeing source code, the url looks like this :
查看源代码时,网址如下所示:
http://localhost/mysite/css/style.css
So can anybody help me out from this issue ?
那么有人可以帮我解决这个问题吗?
Thanks in advance.
提前致谢。
回答by Sagar Gautam
The asset()
helper prepends the base URL to the path you supply.
该asset()
助手预先考虑基础URL给你提供路径。
The base URL is the location of index.php (in this case: http://localhost/mysite/).
基本 URL 是 index.php 的位置(在本例中:http://localhost/mysite/)。
If you don't want index.php inside /public you will need to use public/ inside your asset path, like this:
如果您不想在 /public 中使用 index.php,则需要在资产路径中使用 public/ ,如下所示:
asset("public/css/style.css")
Same for the js files, I hope you understand.
js 文件也一样,希望你理解。
回答by Clément Baconnier
You have 3 possibilites
你有 3 种可能性
- Use a tool like Valet
- Use VirtualHost
- Or the easiest, use the command
php artisan serve
in your project and browse http://localhost:8000see Local Development Server
- 使用Valet 之类的工具
- 使用虚拟主机
- 或者最简单的,
php artisan serve
在你的项目中使用命令并浏览 http://localhost:8000查看本地开发服务器
Edit : I forgot Homestead
编辑:我忘记了宅基地
回答by Jasim Juwel
When cut .htaccess
and index.php
files must give public folder as path.
Try with this:
当剪切.htaccess
和index.php
文件必须给公用文件夹作为路径。试试这个:
css
{{ URL::asset('public/css/style.css') }}
js
{{ URL::asset('public/js/query-1.11.1.min.js') }}
回答by Udhav Sarvaiya
FOR JAVASCRIPT :
对于 JAVASCRIPT :
<script type="text/javascript" src="{{ URL::asset('js/query-1.11.1.min.js') }}"></script>
FOR CSS :
对于 CSS :
<link href="{{ URL::asset('css/style.css') }}" rel="stylesheet" type="text/css" >
NOTE:This will work if your directory structure is like this: /public/css/style.cssOR /public/js/query-1.11.1.min.js
注意:如果您的目录结构是这样的,这将起作用:/public/css/style.cssOR /public/js/query-1.11.1.min.js
回答by dhara gosai
you have directly put your css and js so you have to try this assets href="{{ assets('')}}"
function.
你已经直接把你的css和js,所以你必须尝试这个assetshref="{{ assets('')}}"
功能。
<link href="{{ asset('css/invoice.css') }}" rel="stylesheet" type="text/css"/>
and also js
还有js
<script type="text/javascript" src="{{ asset('js/word.js') }}"></script>
回答by Mubin
create .htaccess on your root folder and write this:
在您的根文件夹上创建 .htaccess 并写入:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^localhost$ [NC]
RewriteRule ^(.*)$ public/ [L,NC]
</IfModule>