ViewComposer不起作用

时间:2017-03-29 12:05:47

标签: php laravel laravel-5

我做了一个header.blade.php,它扩展到许多View页面。在标题菜单项中来自数据库。所以我使用了ViewComposer。

这是我的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()
    {
        //
    }
}

这里是CategoryComposer类:

<?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));
    }
}

我还在config / app

中注册了服务提供商

但问题是我在Undefined variable: categories

等其他网页上遇到错误

你能告诉我这里的错误是什么吗?

0 个答案:

没有答案