如何使用 laravel view composer 排除某些视图

how to exclude certain views with laravel view composer

如何确保通过视图编辑器为 select 视图加载数据,并且只排除少数,具体来说是两个视图?我可以使用正则表达式而不是“*”吗?

public function boot()
{
    view()->composer(
        '*',
        'App\Http\ViewComposers\ProfileComposer'
    );
}

只有两个视图我想避免,它们扩展了其他人使用的相同 blade,不确定声明所有 99 个其他视图是否是最好的 - 如果我可以定义那些被排除在外那就太好了。

也许这不是最好的方法,但它可以这样做

在您的服务提供商中注册您的视图作曲家

public function boot()
{
    view()->composer(
        '*',
        'App\Http\ViewComposers\ProfileComposer'
    );
}

在您的 ProfileComposer 撰写方法视图 class 存储库中有类型提示。用它来获取视图当前名称的名称,并为排除的视图名称做一个条件。

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);
        }
    }
}