Laravel 5查看具有多个变量的作曲家

时间:2015-09-16 17:27:49

标签: php laravel laravel-5

是否有更优雅的做法:

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

使用视图编辑:

NavigationComposer

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

SidebarComposer

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

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

3 个答案:

答案 0 :(得分:0)

如果您只想在主布局中访问部分视图,则可以执行以下操作。

在主布局中,为部分视图或视图创建一个部分:

@section('partial_view_1')
@show
@section('partial_view_2')
@show

在控制器的构造函数中,执行此操作,确保声明$layout

$this -> layout  = view('layouts.master');
$this -> layout -> partial_view_1 = view('includes.top_nav'); 
// -> with($data); if you want to pass data to the top nav. 
$this -> layout -> partial_view_2 = view('includes.side_nav');

public function index(){
    $this -> layout -> main_content = view('contents.main');
    return $this -> layout;
 }

现在,如果要在调用视图时将数据绑定到视图,则可能需要使用View Composer。 希望这有帮助。

答案 1 :(得分:0)

试试这个。这对我有用。

public function boot()
    {
        view::composer('layout', function ($sliders){
            $all_sliders = DB::table('sliders')
                ->where('status', 1)
                ->get();
            $sliders->with('sliders', $all_sliders);
        });

        view::composer('layout', function ($categories){
            $all_category = DB::table('categories')
                ->where('status', 1)
                ->get();
            $categories->with('categories', $all_category);
        });

        view::composer('layout', function ($brands){
            $all_brand = DB::table('brands')
                ->where('status', 1)
                ->get();
            $brands->with('brands', $all_brand);
        });
    }

答案 2 :(得分:0)

您可以在view composer函数中使用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