Class 查看 Laravel 4 中的作曲家

Class View Composers in Laravel 4

我有一个ServiceProviderclass

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 class:

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

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

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

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

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