Laravel 5.7 - CSS 和 JS 不使用 URL::asset 加载

Ank*_*kit 4 php laravel laravel-5.7

我是laravel的新手。我在本地系统上设置了 laravel 5.7 并将 js 和 css 放入公共文件夹,但是当在浏览器上点击我的站点 url 时,站点没有加载任何 css 和 js。

网站结构:

app
bootstrap
config
database
public
 -css
 -js
 -images
resources
 -views
    -includes
    -layouts
    -pages
routes
tests
vendor
index.php
server.php
.env
Run Code Online (Sandbox Code Playgroud)

Note : I have cut .htaccess and index.php files and put these on root to run site without public in path.

Here is how I am calling url : CSS :

{{ URL::asset('css/style.css') }}
Run Code Online (Sandbox Code Playgroud)

JS :

{{ URL::asset('js/query-1.11.1.min.js') }}
Run Code Online (Sandbox Code Playgroud)

When seeing source code, the url looks like this :

http://localhost/mysite/css/style.css
Run Code Online (Sandbox Code Playgroud)

So can anybody help me out from this issue ?

Thanks in advance.

Sag*_*tam 5

asset()助手预先考虑基础URL给你提供路径。

基本 URL 是 index.php 的位置(在本例中:http://localhost/mysite/)。

如果您不想在 /public 中使用 index.php,则需要在资产路径中使用 public/ ,如下所示:

asset("public/css/style.css")
Run Code Online (Sandbox Code Playgroud)

js文件也一样,希望你理解。

  • 您将域`example.com` 指向`index.php` 所在的文件夹`mysite/public`。除了 `asset('public/css/... ');` 将生成 url `example.com/public/css/...` 但由于您的域 **already** 指向 `public` 它会trow 404 因为您的资产实际上位于地址`example.com/css/...` (2认同)