如何解决 laravel 中的视图作曲家问题

How to resovle problem view composer in laravel

config/app.php

'providers' => [
     // ...
     App\Providers\ViewServiceProvider::class,
     App\Http\View\Composers\AdComposer::class,
],

app/Providers/ViewServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;

class ViewServiceProvider extends ServiceProvider
{

    public function register()
    {
        //
    }

    public function boot()
    {
        View::composer(
            'layouts.sidebar', 'App\Http\View\Composers\AdComposer'
        );
    }
}

app/Http/View/Composers/AdComposer.php

<?php

namespace App\Http\View\Composers;

use App\Ad;
use Illuminate\View\View;
use Illuminate\Support\Facades\Request;

class AdComposer
{

    protected $ads;

    public function __construct(Ad $ads)
    {
        $this->ads = $ads;
    }

    public function compose(View $view)
    {
        $view->with('ads', $this->ads);
    }
}

我的问题是:

TypeError Argument 1 passed to App\Http\View\Composers\AdComposer::__construct() must be an instance of App\Ad, instance of Illuminate\Foundation\Application given, called in E:\appoo\vendor\laravel\framework\src\Illuminate\Foundation\ProviderRepository.php on line 208

如何解决这个问题并通过 $ads 来查看?

从供应商数组中删除 AdComposer

文档状态:

Remember, if you create a new service provider to contain your view composer registrations, you will need to add the service provider to the providers array in the config/app.php configuration file.

它不会告诉您将作曲家添加到提供者数组中。

此外,如果您 type-hint App\Ad 您会得到一个空实例,只是为了让您知道。