引导样式在 Laravel 5.2 的某些视图中不起作用

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

bootstrap styling doesn't work in some views in Laravel 5.2

phptwitter-bootstraplaravellaravel-5.2

提问by Ahmed Essam

I'm begginer in Laravel 5.2, I make a simple personal blog and here's views folder

我是 Laravel 5.2 的初学者,我做了一个简单的个人博客,这里是 views 文件夹

Views Folder

视图文件夹

in home.blade.php I extends 'layouts.master' which uses bootstrap and works great. now I made a new route to show specific blog:

在 home.blade.php 中,我扩展了“layouts.master”,它使用引导程序并且效果很好。现在我做了一个新的路线来显示特定的博客:

Route::group(['prefix'=>'post'], function(){
    Route::get('{id}', function(){
        return view('blog');
    });
});

when I go to http://localhost:8000/post/1it works great but the problem when I extend layouts.master bootstrap and my custom css doesn't work ! it works in home.blade.php and works in pages/about.blade.php correctly but when I try to make any new route to a view and extend layouts.master it extends but bootstrap and custom css doesn't work. blog view

当我访问http://localhost:8000/post/1 时,它运行良好,但是当我扩展 layouts.master bootstrap 并且我的自定义 css 不起作用时出现问题!它在 home.blade.php 中工作并且在 pages/about.blade.php 中正确工作,但是当我尝试创建任何新路由到视图并扩展 layouts.master 时,它会扩展但引导程序和自定义 css 不起作用。 博客视图

Have any idea why is that? Notice: my css and bootstrap is in public folder.

知道这是为什么吗?注意:我的 css 和 bootstrap 位于 public 文件夹中。

回答by Ohgodwhy

@ThomasKim Is absolutely right. The path to your bootstrap file is relative. So when you're on localhost:8000it can correctly traverse to css/bootstrap.min.css.

@ThomasKim 绝对正确。引导程序文件的路径是相对的。所以当你在localhost:8000它上面时可以正确地遍历到css/bootstrap.min.css.

However when you're on /post/1then the URL request for the css file changes to:

但是,当您打开时/post/1,css 文件的 URL 请求将更改为:

localhost:8000/post/1/css/bootstrap.min.css

And we know that's incorrect. To resolve this, use an absolute path by prepending a /.

我们知道这是不正确的。要解决此问题,请通过在/.

<link href="/css/bootstrap.min.css" rel="stylesheet"/>

This will ensure that it always attempts to traverse the file from public/css/bootstrap.min.css. publicis omitted in the URL structure, but that is where all HTTP requests resolve to.

这将确保它始终尝试从 public/css/bootstrap.min.css 遍历文件。public在 URL 结构中省略了,但这是所有 HTTP 请求解析的地方。

UPDATE

更新

It would actually be best to use Laravel's assetor secure_assetfunctions to resolve these assets instead:

实际上最好使用 Laravelassetsecure_asset函数来解析这些资产:

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

The assethelper will resolve directly from /public

asset助手将直接从解决/public

Note that assetwill use the schemaof the page request to resolve the dependency, whereas secure_assetwill force the dependency over httpsno matter what the schema of the request is.

请注意,asset将使用schema页面请求的 来解决依赖关系,而无论请求的架构是什么,secure_asset都将强制依赖关系https

回答by Wes Dollar

To make life super easy, use Laravel's asset() helper:

为了让生活变得超级简单,请使用 Laravel 的 asset() 助手:

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

Yes, it provides the exact same thing as referencing it as simply "/css/..." but I find it to be good practice, especially when things get a bit more advanced later down the line.

是的,它提供了与简单地将它引用为“/css/...”完全相同的东西,但我发现这是一个很好的做法,尤其是当事情变得更先进时。

回答by Gaurav Kumar

You must have used

你一定用过

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

in your main blade layout template, that why facing this problem

在您的主刀片布局模板中,为什么要面临这个问题

Use

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

and this will resolve the issue.

这将解决问题。

Just append a forward slash /before css file path, which indicates the public directory of your project.

只需/在 css 文件路径前附加一个正斜杠,它表示您的项目的公共目录。