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

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

Assets not referencing to public folder (Laravel)

phplaravel

提问by Gabriel Augusto

I have the public folder inside my laravel project and I have some js and css files inside it.

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

I'm using the asset function and even though it's referencing to the public folder, my files aren't loaded on the page.

我正在使用资产功能,即使它引用了公共文件夹,我的文件也没有加载到页面上。

I'm using this code to load (it's only one example, there are more files):

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

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

And on the browser's console, I'm geting something like this:

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

Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:8000/css/style.css

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

Well, I tried to revert the last commit, but no success. Tried to change to URL::asset() function, nothing. Tried everything from the following link: http://laravel.io/forum/09-17-2014-problem-asset-not-point-to-public-folder?page=1and success.

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

Please, a little help?

拜托,有点帮助?

Thanks!

谢谢!

回答by Bhojendra Rauniyar

I was having same problem. This is due to moving of .htaccess file from public to root of the project in order to serve localhost/projectrather than localhost/project/laravel. And needed to use public as well in the asset:

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

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

Or, modify the asset function from /Illuminate/Foundation/helpers.php

或者,从 /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);
    }
}

The preceding method is not good way. Anyway this would have been easier if there was config for setting asset path.

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

回答by Dilajani Kotavadekar

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

Try this

尝试这个

回答by Derk Jan Speelman

After you've savely and succesfully setup your .htaccess(like this), your method will work as you would expect it to work:

在您保存并成功设置您的.htaccess像这样)之后,您的方法将按您期望的那样工作:

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

Be aware that allclient-side URL requests (requests to images, JavaScript/CSS files, JSON) will be affected by the Rewrite Rules after you've setup your .htaccess.

请注意,所有客户端 URL 请求(对图像的请求、JavaScript/CSS 文件、JSON)在您设置.htaccess.

Also be aware that it might take a while after you see the changes. Cache might interfere with these changes. So, make sure you clear your Laravel cache:

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

In your Shell run the following commands individually:

在您的 Shell 中分别运行以下命令:

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

And make sure to disable caching temporarily in your browser settings:

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

In Google Chrome: Open Dev Tools> Open the Network tab> Check "Disable cache".

在 Google Chrome 中:打开开发工具>打开网络选项卡> 选中“禁用缓存”

Other browsers: search online how to do that.

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