Laravel 4中的班级视图作曲家

时间:2015-03-17 17:36:46

标签: php laravel view composer-php

我有ServiceProvider

class ComposerServiceProvider extends ServiceProvider {

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind(CategoryListComposer::class, function ()
        {
            new CategoryListComposer($this->app->make(CategoryInterface::class));
        });
    }

    /**
     * Bootstrap the application events.
     *
     * @return void
     */
    public function boot()
    {
        $this->app->view->composer(
            'frontend.layouts.partials.header',
            $this->app->make(CategoryListComposer::class)
        );
    }
}

CategoryListComposer课程:

public function compose($view)
{
    dd(567);
}

当我运行我的应用程序时,567无法打印出来。

1 个答案:

答案 0 :(得分:1)

假设您已经在CategoryListComposer课程中提示类型暗示了构造函数依赖项,那么您不需要在register方法中执行所有绑定。

只需从register方法中删除代码(Laravel要求该方法存​​在,即使它未被使用),并将boot方法更改为:

public function boot()
{
    $this->app->view->composer(
        'frontend.layouts.partials.header',
        CategoryListComposer::class
    );
}