dongtan1845 2016-02-29 22:04
浏览 35
已采纳

Laravel - 在所有路线上请求提供商数据

I am currently using a Laravel Provider to pass data to a view each time it is called. The App.blade.php includes the blade file if the user is authenticated.

My problem is that at the moment, no matter what view the user is on, it still calls the ViewServiceProvider.php, which doesn't seem very efficient.

I have tried to use @if(view()->exists('home')), but that doesn't seem to have any effect what so ever, and thus, the queries are still called from the ViewServiceProvider.php.

App.blade.php:

@if(!Auth::guest())

  @if(view()->exists('home'))

    @include('layouts.check')

  @endif

@endif

ViewServiceProvider.php:

public function boot()
    {
      view()->composer('layouts.check', function ($view) {

        $sites = Site::where('trust_id', Auth::id())->get();

        $view->with(['sites' => $sites]);
      });
    }

Any help would be hugely appreciated.

  • 写回答

2条回答 默认 最新

  • dongxingji3882 2016-02-29 22:32
    关注

    The problem with you code is that it's check to see if the view exists. Chances are, the view "home" will always exist, so it will always include your view; "layouts.check".

    Unless, of course, the view "home" is dynamic and is only there conditionally, which doesn't seem right. If you want the "layouts.check" view file to only load on certain pages, you might want to try "Request::is()".

    @if(Request::is('home'))
        @include('layouts.check')
    @endif
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助