mercredi 29 mars 2017

ViewComposer Doesn't Work

I have made a header.blade/php which is extended to many View pages. in header menu items come from database.So I used ViewComposer.

Here is my ComposerServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;

class ComposerServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {

         View::composer(['cart.layouts.header'], 'App\Http\ViewComposers\CategoryComposer@compose');
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

here is the CategoryComposer Class:

<?php

namespace App\Http\ViewComposers;

use Illuminate\View\View;

class CategoryComposer
{
    public $categoryList = [];
    /**
     * Create a movie composer.
     *
     * @return void
     */
    public function __construct()
    {
        $this->categoryList = \DB::table('categories')
                            ->select('categories.*')
                            ->get();
    }

    /**
     * Bind data to the view.
     *
     * @param  View  $view
     * @return void
     */
    public function compose(View $view)
    {
        $view->with('categories', end($this->categoryList));
    }
}

I have also registered the Service Provider in config/app

But the problem is I got error in other pages like Undefined variable: categories

Can you suggest me what could be the Error here?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire