gpt4 book ai didi

php - Laravel 5 具有多个变量的 View 编辑器

转载 作者:行者123 更新时间:2023-12-01 22:50:08 24 4
gpt4 key购买 nike

有没有更优雅的做法:

abstract class Controller extends BaseController
{
use DispatchesJobs, ValidatesRequests;

function __construct() {
view()->composer('includes.topbar', function ($view) {
$view->with('nav', $this->topNav());
});
view()->composer('includes.sidebar', function ($view) {
$view->with('sidebar', $this->sidebar());
});
}
...
}

<小时/>

使用 View 编辑器:

导航 Composer

 namespace App\Http\ViewComposers;

use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Auth;

class NavigationComposer
{
protected $user;

public function __construct()
{
$this->user = Auth::user();
}

public function compose(View $view)
{
$view->with(['nav' => $this->topNav(), 'sidebar' => $this->sidebar()]);
}

public function topNav()
{
$nav['Dashboard'] = [
'route' => 'dashboard'
];
$nav['Sales'] = [
'route' => 'sales',
'subitems' => [
'Enquiries' => 'sales.enquiries',
'Quotes' => 'sales.quotes',
'Orders' => 'sales.orders'
]
];
$nav['CRM'] = [
'route' => 'crm',
'subitems' => [
'Companies' => 'crm.companies',
'Conversion report' => 'crm.conversion-report'
]
];
return $nav;
}

public function sidebar()
{
return 'sidebar';
}
}

ComposerServiceProvider

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class ComposerServiceProvider extends ServiceProvider
{
/**
* Register bindings in the container.
*
* @return void
*/
public function boot()
{
// Using class based composers...
view()->composer(
'includes.topbar', 'App\Http\ViewComposers\NavigationComposer'
);
view()->composer(
'includes.sidebar', 'App\Http\ViewComposers\NavigationComposer'
);
}
...
}
<小时/>

我按照@Gal 的建议将它们分成两部分

ComposerServiceProvider

...
public function boot()
{
// Using class based composers...
view()->composer('includes.topbar', 'App\Http\ViewComposers\TopNavigationComposer');
view()->composer('includes.sidebar', 'App\Http\ViewComposers\SidebarComposer');
}
...

TopNavigationComposer

...
public function compose(View $view)
{
$view->with('nav', $this->topNav());
}

public function topNav()
{
$nav['Dashboard'] = [
'route' => 'dashboard'
];
$nav['Sales'] = [
'route' => 'sales',
'subitems' => [
'Enquiries' => 'sales.enquiries',
'Quotes' => 'sales.quotes',
'Orders' => 'sales.orders'
]
];
$nav['CRM'] = [
'route' => 'crm',
'subitems' => [
'Companies' => 'crm.companies',
'Conversion report' => 'crm.conversion-report'
]
];
return $nav;
}
...

侧边栏 Composer

...
public function compose(View $view)
{
$view->with('sidebar', $this->sidebar());
}

public function sidebar()
{
return 'sidebar';
}
...

最佳答案

您可以在 View 编辑器函数中使用多个 with 链来检索变量。

//For all views
\View::composer('*', function($views){
$views->with('users', App\User::all())
->with('roles', App\Role::all())
->with('publishers', Publisher::all());
});

了解更多信息:Laravel 5.7 View Composer

关于php - Laravel 5 具有多个变量的 View 编辑器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32615022/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com