未引用公用文件夹 (Laravel) 的资产


Assets not referencing to public folder (Laravel)

我的laravel项目中有一个公共文件夹,里面有一些js和css文件。

我正在使用 asset 函数,即使它引用了公用文件夹,我的文件也不会加载到页面上。

我正在使用此代码进行加载(这只是一个例子,还有更多文件(:

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

在浏览器的控制台上,我得到了这样的东西:

加载资源失败:服务器响应状态为 404(未找到( http://localhost:8000/css/style.css

好吧,我试图恢复最后一次提交,但没有成功。尝试更改为URL::asset((函数,什么都没有。尝试了以下链接中的所有内容:http://laravel.io/forum/09-17-2014-problem-asset-not-point-to-public-folder?page=1 和成功。

请帮忙吗?

谢谢!

我遇到了同样的问题。这是由于将.htaccess文件从公共文件移动到项目的根目录,以便为localhost/project而不是localhost/project/laravel提供服务。并且需要在资产中使用公共:

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

或者,从/Illuminate/Foundation/helpers 修改资源函数.php

if (! function_exists('asset')) {
    /**
     * Generate an asset path for the application.
     *
     * @param  string  $path
     * @param  bool    $secure
     * @return string
     */
    function asset($path, $secure = null)
    {
        return app('url')->asset("public/".$path, $secure);
    }
}

前面的方法不是好方法。无论如何,如果有用于设置资产路径的配置,这会更容易。

添加您的env

ASSET_URL=public 

ASSET_URL=public/

ASSET_URL=/public

你需要哪一个你可以做到...

在保存

并成功设置.htaccess(如下所示(后,您的方法将按预期工作:

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

请注意,设置.htaccess后,所有客户端 URL 请求(对图像、JavaScript/CSS 文件、JSON 的请求(都将受到重写规则的影响。

另请注意,看到更改后可能需要一段时间。缓存可能会干扰这些更改。因此,请确保清除Laravel缓存:

在命令行管理程序中,分别运行以下命令:

php artisan cache:clear
php artisan route:cache
php artisan config:cache
php artisan view:clear

并确保在浏览器设置中暂时禁用缓存:

在谷歌浏览器中:打开开发工具>打开网络选项卡>选中"禁用缓存"。

其他浏览器:在线搜索如何做到这一点。

在我的情况下,我尝试了.env

ASSET_URL=public

双斜杠和单独,没有任何帮助以及所有相关的 SO 答案,然后打开我的旧项目并看到了。这最终对我有用(当然,在清除视图和缓存旁边(

ASSET_URL="${APP_URL}"

我说的是本地主机端

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

试试这个

添加这个在你的.htaccess文件中

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]

它将解决您的问题。确定

就我而言,我在 laravel 项目的环境文件 (.env( 中进行了这些更改

APP_URL=https://sitename.com
ASSET_URL=https://sitename.com/public

在当地一切都很好...但是当我将我的 laravel项目转移到生产环境时,资产无法正确加载......然后这帮助我解决了这个问题...

由于您已将.htaccess文件移动到根文件夹,因此无论使用何处,都必须更改资产URL。网址应如下所示,

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

或者,您可以在 .env 文件中将ASSET_URL设置为"公共",

ASSET_URL=http://localhost/projectname/public

对不起我的英语,我在使用 mike42/escpos-php 的情况下遇到了同样的问题,因为在 xampp 中

我移动到 zlib.output_compression=on

正确的方法是

zlib.output_compression=关闭

资产再次工作

stlye.css正在从公共文件夹加载并为我工作。不要忘记添加rel="stylesheet"

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

我们可以直接在资产函数中传递公众。这是一个例子

<!DOCTYPE html>
<html lang="en" class="h-full">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <link rel="shortcut icon" href="{{ asset(mix('img/log-viewer-32.png', 'vendor/log-viewer')) }}">
    <title>Log Viewer{{ config('app.name') ? ' - ' . config('app.name') : '' }}</title>
    <!-- Style sheets-->
    <link href="{{ asset(mix('app.css', 'vendor/log-viewer')) }}" rel="stylesheet" onerror="alert('app.css failed to load. Please refresh the page, re-publish Log Viewer assets, or fix routing for vendor assets.')">
</head>
<body class="h-full px-3 lg:px-5 bg-gray-100 dark:bg-gray-900">
<div id="log-viewer" class="flex h-full max-h-screen max-w-full">
    <router-view></router-view>
</div>
<!-- Global LogViewer Object -->
<script>
    window.LogViewer = @json($logViewerScriptVariables);
    // Add additional headers for LogViewer requests like so:
    // window.LogViewer.headers['Authorization'] = 'Bearer xxxxxxx';
</script>
<script src="{{ asset(mix('app.js', 'vendor/log-viewer')) }}" onerror="alert('app.js failed to load. Please refresh the page, re-publish Log Viewer assets, or fix routing for vendor assets.')"></script>
</body>
</html>

连接应用程序.css和应用程序后.js它没有正确加载,所以我在资产功能中添加了公共,它工作得很好

<link href="{{ asset('public' . mix('app.css', 'vendor/log-viewer')) }}" rel="stylesheet" onerror="alert('app.css failed to load. Please refresh the page, re-publish Log Viewer assets, or fix routing for vendor assets.')">
<script src="{{ asset('public' . mix('app.js', 'vendor/log-viewer')) }}" onerror="alert('app.js failed to load. Please refresh the page, re-publish Log Viewer assets, or fix routing for vendor assets.')"></script>

并确保在浏览器设置中暂时禁用缓存:

在谷歌浏览器中:

Open Dev Tools > Open the Network tab > Check "Disable cache".

其他浏览器:在线搜索如何做到这一点。

这对我有用。似乎CSS和JS的先前加载在缓存中,并且在发布之前不会加载新的加载