php Laravel 5 找不到 css 文件

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

Laravel 5 not finding css files

php.htaccesslaravellaravel-5

提问by davelowe85

I've just installed a Laravel 5 project on MAMP and my pages are not finding the css files.

我刚刚在 MAMP 上安装了 Laravel 5 项目,但我的页面没有找到 css 文件。

This is the link to my css in my app.blade.php file:

这是我的 app.blade.php 文件中我的 css 的链接:

<link href="/css/app.css" rel="stylesheet">

And my .htaccess file has this line:

我的 .htaccess 文件有这一行:

RewriteBase /laravel-site/laravel-5-app/public/

In the config folder my app.php file contains this:

在 config 文件夹中,我的 app.php 文件包含以下内容:

'url' => 'http://localhost:8888/laravel-site/laravel-5-app/public/',

But when I open up this page: http://localhost:8888/laravel-site/laravel-5-app/public/auth/loginand check the developer tools, it is looking for the css file at this location: http://localhost:8888/css/app.css

但是当我打开这个页面:http://localhost:8888/laravel-site/laravel-5-app/public/auth/login并检查开发者工具时,它正在这个位置寻找 css 文件:http: //本地主机:8888/css/app.css

Just as a side note, if I go to this url: http://localhost:8888/laravel-site/laravel-5-app/public/I get the correct welcome page.

顺便提一下,如果我访问这个 url:http://localhost:8888/laravel-site/laravel-5-app/public/,我会得到正确的欢迎页面。

回答by Kalhan.Toress

Use this to add assets like css, javascript, images.. into blade file.

使用它可以将css, javascript, images.. 之类的资产添加到刀片文件中。

FOR CSS,

对于 CSS,

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

OR

或者

<link href="{{ URL::asset('css/app.css') }}" rel="stylesheet" type="text/css" >

FOR JS,

对于 JS,

<script type="text/javascript" src="{{ asset('js/custom.js') }}"></script>

OR

或者

 <script type="text/javascript" src="{{ URL::asset('js/custom.js') }}"></script>

FOR IMAGES,

对于图像,

{{ asset('img/photo.jpg'); }}

Here is the DOC

这是DOC

Alternatively, if you pulled the composer package illuminate/htmlwhich was come as default in laravel 4.2 then you can use like below, In laravel 5. you have to manually pull the package.

或者,如果你拉取illuminate/html了 laravel 4.2 中默认的 composer 包,那么你可以像下面这样使用,在 laravel 5. 你必须手动拉取包。

{{ HTML::style('css/style.css') }}

Here is an Example.

这是一个例子

回答by Thilina Dharmasena

In the root path create a .htaccess file with

在根路径中创建一个 .htaccess 文件

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_URI} !^/public/ 

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f



RewriteRule ^(.*)$ /public/ 
#RewriteRule ^ index.php [L]
RewriteRule ^(/)?$ public/index.php [L] 
</IfModule>

In public directory create a .htaccess file

在公共目录中创建一个 .htaccess 文件

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

if you have done any changes in public/index.php file correct them with the right path then your site will be live.

如果您在 public/index.php 文件中进行了任何更改,并使用正确的路径更正它们,那么您的站点就会上线。

what will solve with this?

这将解决什么问题?

  • Hosting issue of laravel project.
  • Css not work on laravel.
  • Laravel site not work.
  • laravel site load with domain/public/index.php
  • laravel project does not redirect correctly
  • Laravel 项目的托管问题。
  • CSS 不适用于 Laravel。
  • Laravel 网站不工作。
  • laravel 站点加载 domain/public/index.php
  • laravel 项目没有正确重定向

回答by Pervez

You can use one of the following options:

您可以使用以下选项之一:

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


<link href="{{ URL::asset('css/app.css') }}" rel="stylesheet" type="text/css" >


{!! Html::style( asset('css/app.css')) !!}

回答by David J

Stop artisan serve

Stop artisan serve

and try using

并尝试使用

php -S localhost:8000 -t public

回答by ThuanLe

It's works. For css:

其作品。对于 CSS:

<link href="css/app.css" rel="stylesheet">

Try to:

尝试:

 <link href="public/css/app.css" rel="stylesheet">

回答by Uchiha Madara

To fast apply simple regex.

快速应用简单的正则表达式。

search : href="(.+?)"
replace : href="{{ asset('') }}"

and

search : src="(.+?)"
replace : src="{{ asset('') }}"

回答by Mansoor Ahmad

Here is the solution

这是解决方案

  {!! Html::style('css/select2.min.css') !!}
  {!! Html::script('js/select2.min.js') !!}

回答by Muddasir Abbas

This works for me

这对我有用

If you migrated .htaccess file from public directory to project folder and altered server.php to index.php in project folder then this should works for you.

如果您将 .htaccess 文件从公共目录迁移到项目文件夹并将 server.php 更改为项目文件夹中的 index.php 那么这应该适合您。

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

Thanks

谢谢

回答by Romelle

I was having the same problem, until just now.

我遇到了同样的问题,直到现在。

Removing the /from before /css/app.cssso that its css.app.cssworked for me.

/从之前删除它, /css/app.css以便它对css.app.css我有用。

回答by AlamiFati

if you are using laravel 5 or 6:

如果您使用的是 laravel 5 或 6:

  1. inside public folder create css,js,images... foldersenter image description here

  2. then inside your blade file you can display an images using this code :

  1. 在公共文件夹中创建 css,js,images... 文件夹在这里输入图片描述

  2. 然后在您的刀片文件中,您可以使用以下代码显示图像:

<link rel="stylesheet" href="/css/bootstrap.min.css">
<link src="/images/test.png">
<!-- / is important and dont write public folder-->