绑定View :: composer以使用通配符匹配所有视图吗? [英] Binding View::composer to match all views using wildcards?
问题描述
我有一个这样的导航栏.
I have a navigation bar like this.
<li>Account</li>
<ul>
<li>Register</li>
<li>Login/li>
...
我想根据Auth::check()
动态地更新它.例如,如果用户已登录,则帐户"将更改为我的个人资料页面",子兄弟姐妹将替换为适当的数组.
I want to update this dynamically depending on Auth::check()
. For example, if the user is logged in, "Account" will be changed with "My Profile Page" and child siblings will be replaced with an appropriate array.
我需要在控制器中不编辑View::make calls
的情况下执行此操作.看起来很糟糕.
I need to do this without editing View::make calls
in my controllers. It looks pretty bad.
我正在寻找这样的解决方案;
A solution like this is what I'm looking for;
View::composer('home.*', function($view) {
if(Auth::check())
return $view->nest('accountArea', 'home.navigation-loggedIn', null);
else
return $view->nest('accountArea', 'home.navigation-visitor', null);
});
如果有更好的选择,我也想听听他们!
If there are better alternatives, I would like to hear them too!
推荐答案
似乎类似于Laravel中的通配符.到目前为止,它们还没有记录.
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);
});
这就是我想要的.
更新:这是替代解决方案
您还可以将其绑定到布局,因此扩展该布局的所有子视图都将从composer中受益.
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);
});
它将作曲家绑定到每个执行@extend('admin.layouts.main_layout')
的视图.
It will bind composers to every view that does @extend('admin.layouts.main_layout')
.
这篇关于绑定View :: composer以使用通配符匹配所有视图吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!