也許它不是這樣做的最佳方式,但註冊的View Composer
public function boot()
{
view()->composer(
'*',
'App\Http\ViewComposers\ProfileComposer'
);
}
在你ProfileComposer
撰寫方法View類倉庫的類型是暗示它可以像這樣
您的服務提供商來完成。使用它來獲取當前的視圖名稱併爲排除的視圖名稱創建條件。
class ProfileComposer
{
public function __construct()
{
// Dependencies automatically resolved by service container...
}
/**
* Bind data to the view.
*
* @param View $view
* @return void
*/
public function compose(View $view)
{
$excludedViews = ['firstView','SecondView'];
//Check if current view is not in excludedViews array
if(!in_array($view->getNmae() , $excludedViews))
{
$view->with('dataName', $this->data);
}
}
}