Laravel Blade 指令将“public”文件夹中的 CSS 样式正确附加到模板

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

Laravel Blade directive to properly attach to template a CSS style from 'public' folder

phpcsslaravelblade

提问by Diogo Franco

i'm a laravel newbie, and i'm trying to import a css file into my blade template in the views folder.

我是 Laravel 新手,我正在尝试将一个 css 文件导入到我的视图文件夹中的刀片模板中。

I have this in the routes file:

我在路由文件中有这个:

Route::get('/', function()
{
return View::make('index');
});

index.blade.php in the 'views' folder has the following code in the header section:

“views”文件夹中的 index.blade.php 在标题部分具有以下代码:

{{HTML::style('css/common.css');}}
</head>

common.css is a file in public folder, inside a css subfolder.

common.css 是 public 文件夹中的一个文件,在 css 子文件夹中。

The index.blade.php view runs without errors, showing up all the html elements, but without the css. What am i missing here? Any help would be much appreciated.

index.blade.php 视图运行没有错误,显示所有 html 元素,但没有 css。我在这里错过了什么?任何帮助将非常感激。

Edit: It looks like the problem has to do with some .htaccess or xampp configuration. The href generated in the page shows [http://localhost/testLaravel/server.php/css/common.css], and that is why it doesn't get the css code. The correct path would be [http://localhost/testLaravel/public/css/common.css].

编辑:看起来问题与某些 .htaccess 或 xampp 配置有关。页面中生成的href显示为[http://localhost/testLaravel/server.php/css/common.css],这就是为什么它没有获取css代码的原因。正确的路径是 [http://localhost/testLaravel/public/css/common.css]。

mod_rewrite is on and my .htacess file is:

mod_rewrite 已打开,我的 .htacess 文件是:

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

RewriteEngine On

# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ / [L,R=301]

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

Any help getting rid of the server.php portion of the path would be much appretiated.

任何摆脱路径的 server.php 部分的帮助都会非常有用。

回答by Gadoma

try updating your template with the below fragment

尝试使用以下片段更新您的模板

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

or manual way

或手动方式

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

The asset()function is a 'shortcut' for the public folder path.

asset()函数是公用文件夹路径的“快捷方式”。