【问题标题】:How to change different header for various pages in laravel如何在laravel中更改各个页面的不同标题
【发布时间】:2018-10-05 22:16:00
【问题描述】:
我需要调用不同的标题来托管和关于下面提到的页面,在执行下面的代码时,我收到错误为“未定义的类常量'托管'”。建议我如何解决这个问题并为各个页面调用不同的标题。
@if(Route::hosting == 'hosting')
{
@include('partials.header');
}
@elseif(Route::About == 'About'){
@include('partials.header1');
}
@endif
【问题讨论】:
标签:
laravel
laravel-5
laravel-5.2
laravel-5.1
【解决方案1】:
使用\Request::is()检查当前路线,不要在@if条件中使用{...},如果只有两个条件@if...@else...@endif就足够了。
@if(\Request::is("hosting"))
@include('partials.header');
@else
@include('partials.header1');
@endif
你也可以避免使用另一个标题 partials.header1,如果没有太大区别。
将名为is_hosting 的变量作为true/false 传递并相应地显示内容..
@if(\Request::is("hosting"))
@include('partials.header',["is_hosting"=>true]);
@else
@include('partials.header',["is_hosting"=>false]);
@endif
内部partials.header
@if(isset($is_hosting) && $is_hosting)
header's content
@else
header1's content
@endif
【解决方案2】:
@if(Route::currentRouteName() == 'hosting')
@include('partials.header');
@elseif(Route::currentRouteName() == 'About')
@include('partials.header1');
@endif
使用Route::currentRouteName()获取当前路由的名称。