Reputation: 2340
How can I make sure that I load data via view composer for select views and only exclude a few, two views to be specific? Can i use a regex instead of '*'?
public function boot()
{
view()->composer(
'*',
'App\Http\ViewComposers\ProfileComposer'
);
}
There are just two views i'd like to avoid, they extend the same blade used by others, not sure declaring all the 99 others would be the best - if i can just define the ones to be left out that'd be great.
Upvotes: 5
Views: 2780
Reputation: 2293
Perhaps it is not the best way for doing this but it can do like this
In your services provider register your view composer
public function boot()
{
view()->composer(
'*',
'App\Http\ViewComposers\ProfileComposer'
);
}
In Your ProfileComposer
compose method View class repository is type hinted. Use it to get the name of the current name of the view and make a condition for excluded view name.
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->getName() , $excludedViews))
{
$view->with('dataName', $this->data);
}
}
}
Upvotes: 6