我目前使用Laravel Provider
在每次调用时将数据传递到视图。如果用户通过身份验证,App.blade.php
将包含刀片文件。Laravel - 在所有路由上请求的提供程序数据
我的问题是,目前,无论用户在哪个视图上,它仍然会调用ViewServiceProvider.php
,这看起来效率不高。
我试图使用@if(view()->exists('home'))
,但这似乎没有任何影响,因此,查询仍然从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]);
});
}
任何帮助将非常感激。
运行ViewServiceProvider似乎很正常,因为它需要注册视图编辑器。但是,只有在使用layouts.check时才能执行闭包。如果你有一个称为home的视图,那么存在的视图总是成立的,因为它检查存在而不是存在。 – vonec