【发布时间】:2017-01-20 22:43:22
【问题描述】:
我正在向我的系统添加用户通知。为了访问用户的这些通知,我调用了我在另一个系统中创建的 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
但是,如果我现在访问仪表板页面之外的另一个页面,我会得到一个未定义的变量:通知错误。这是因为标题在每个页面上,但我只是将通知对象传递给仪表板页面。
有没有办法让这个通知对象普遍可用?
谢谢
更新
if($user = Sentinel::getUser()) {
view()->composer('*', function ($view) {
$view->with('notifications', MyAPI::returnNotifications($user->id));
});
}
【问题讨论】:
标签: laravel laravel-5.2