시스템에 사용자 알림을 추가하고 있습니다. 사용자에 대한 이러한 알림에 액세스하려면 다른 시스템에서 만든 API를 호출합니다. 그래서, 내 IndexController는 다음과 같이 보입니다.응용 프로그램 전체에서 개체에 액세스 가능하게 만들기
public function index()
{
if ($user = Sentinel::getUser()) {
$notifications = MyAPI::returnNotifications($user->id);
return view('centaur.dashboard', compact('notifications'));
}
}
이제는 대시 보드 뷰에서만 알림을 사용할 수 있습니다. 내 헤더보기 내 나는이
@if($notifications)
@foreach($notifications as $notification)
<a class="content" href="#">
<div class="notification-item">
<h4 class="item-title">{{ $notification->subject }}</h4>
<p class="item-info">{{ $notification->body }}</p>
</div>
</a>
@endforeach
@endif
같은 뭔가를하지만 이제 대시 보드 페이지 이외의 다른 페이지를 방문하면 나는 정의되지 않은 변수를 얻을 : 알림 오류가 발생했습니다. 헤더는 모든 페이지에 있지만 알림 개체는 대시 보드 페이지에만 전달하기 때문입니다.
이 알림 개체를 보편적으로 사용할 수있게하는 방법이 있습니까?
감사
UPDATE는
if($user = Sentinel::getUser()) {
view()->composer('*', function ($view) {
$view->with('notifications', MyAPI::returnNotifications($user->id));
});
}
가능하면 중복 될 수 있습니다. http://stackoverflow.com/questions/29715813/laravel-5-global-blade-view-variable-available-in-all-templates – matiit