我有一个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
);
}