资产未引用公用文件夹(Laravel) [英] Assets not referencing to public folder (Laravel)

查看:87
本文介绍了资产未引用公用文件夹(Laravel)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在laravel项目中有公用文件夹,并且其中有一些js和CSS文件.

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

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

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:

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

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

好吧,我试图还原上一次提交,但是没有成功.试图更改为URL :: asset()函数,什么也没有.尝试了以下链接中的所有内容:

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=1 and success.

请帮忙吗?

谢谢!

推荐答案

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

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/project rather than localhost/project/laravel. And needed to use public as well in the asset:

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

或者,从/Illuminate/Foundation/helpers.php修改资产功能

Or, modify the asset function from /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.

这篇关于资产未引用公用文件夹(Laravel)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆