bootstrap样式在Laravel 5.2中的某些视图中不起作用 [英] bootstrap styling doesn't work in some views in Laravel 5.2
问题描述
我是Laravel 5.2的初学者,我做了一个简单的个人博客,这里是views文件夹
I'm begginer in Laravel 5.2, I make a simple personal blog and here's views folder
在home.blade.php中,我扩展了使用引导程序的"layouts.master",并且效果很好. 现在,我做了一条显示特定博客的新路线:
in home.blade.php I extends 'layouts.master' which uses bootstrap and works great. now I made a new route to show specific blog:
Route::group(['prefix'=>'post'], function(){
Route::get('{id}', function(){
return view('blog');
});
});
当我转到 http://localhost:8000/post/1 时,它的效果很好,但当我扩展layouts.master引导程序和我的自定义CSS时,问题不起作用! 它可以在home.blade.php中正常工作,并且可以在pages/about.blade.php中正常工作,但是当我尝试为视图建立任何新路径并扩展layouts.master时,它会扩展,但是自举和自定义CSS无效.
when I go to http://localhost:8000/post/1 it 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.
你知道为什么吗? 注意:我的CSS和引导程序位于公用文件夹中.
Have any idea why is that? Notice: my css and bootstrap is in public folder.
推荐答案
@ThomasKim绝对正确.引导程序文件的路径是相对的.因此,当您使用localhost:8000
时,它可以正确地遍历到css/bootstrap.min.css
.
@ThomasKim Is absolutely right. The path to your bootstrap file is relative. So when you're on localhost:8000
it can correctly traverse to css/bootstrap.min.css
.
但是,当您使用/post/1
时,css文件的URL请求将更改为:
However when you're on /post/1
then the URL request for the css file changes to:
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"/>
这将确保它始终尝试遍历public/css/bootstrap.min.css中的文件. URL结构中省略了public
,但这是所有HTTP请求都解析的位置.
This will ensure that it always attempts to traverse the file from public/css/bootstrap.min.css. public
is omitted in the URL structure, but that is where all HTTP requests resolve to.
更新
实际上最好使用Laravel的asset
或secure_asset
函数来解析这些资产:
It would actually be best to use Laravel's asset
or secure_asset
functions to resolve these assets instead:
<link href="{{ asset('css/bootstrap.min/css') }} rel="stylesheet"/>
asset
助手将直接从/public
请注意,asset
将使用页面请求的schema
来解决依赖关系,而无论请求的模式是什么,secure_asset
都将强制依赖于https
.
Note that asset
will use the schema
of the page request to resolve the dependency, whereas secure_asset
will force the dependency over https
no matter what the schema of the request is.
这篇关于bootstrap样式在Laravel 5.2中的某些视图中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!