Laravel中的类视图编辑器4


Class View Composers in Laravel 4

我有一个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无法打印出来。

假设您已经在CategoryListComposer类中键入了暗示的构造函数依赖项,则不需要在register方法中执行所有绑定。

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

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