Binding View::composer to match all views using wildcards?

纵然是瞬间 提交于 2019-12-04 05:04:59

Seems like the wildcards in Laravel works. They're just undocumented as of now.

View::composer('admin.layouts.*', function($view)
{
     if (Sentry::check()) $view->with('navigation', View::make('admin._partials.navigation'));
     else                 $view->with('navigation', null);
});

That's what I was looking for.

Update: Here is an alternative solution

You can also bind it to the layout, so all the subviews that extend that layout will benefit from composer.

View::composer('admin.layouts.main_layout', function($view)
{
     if (Sentry::check()) $view->with('navigation', View::make('admin._partials.navigation'));
     else                 $view->with('navigation', null);
});

It will bind composers to every view that does @extend('admin.layouts.main_layout').

You can use View::share('variable', 'value') to share a variable across all views.

Like Aristona says (thanks for de advice!): wildcards are allowed. Glancing at the code, we can see how Composers are Event Listeners, and in that section of the documentation is pointed out: Wildcard event listeners.

Adding a little bit more, ultimately, Str::is() is used by Events\Dispatcher to detect wildcar listeners. For example, something like this:

str_is('namespace::*.view', 'namespace::folder.view')

In short, I agree that would not hurt a small informative phrase :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!