【问题标题】: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()获取当前路由的名称。

      【讨论】:

        【解决方案3】:

        看看这个答案:

        How to get current route name in laravel 5?

        我相信您对如何访问当前路线名称感到困惑。有几种方法可以访问当前路由:

        Route::getCurrentRoute()->getPath();  // Route path
        Request::route()->getName();  // Route Name
        $request->path();  // URL
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-10-30
          • 1970-01-01
          • 2014-09-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多