Laravel Artisan event:generate 命令抛出致命错误

Laravel Artisan event:generate command throwing a Fatal error

我正在关注 Laravel 5.7 关于事件注册和生成的官方文档:https://laravel.com/docs/5.7/events#generating-events-and-listeners

我有一个 EventServiceProvider 定义了以下事件:

<?php

namespace App\Providers;

use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{

    /**
     * The event handler mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        'App\Events\FormBeforeCreate' => [
            'App\Listeners\WebhookBeforeCreate',
        ],
        'App\Events\FormAfterCreate' => [
            'App\Listeners\NotifyAfterCreate',
            'App\Listeners\WebhookAfterCreate',
        ],
        'App\Events\FormBeforeUpdate' => [
            'App\Listeners\WebhookBeforeUpdate',
        ],
        'App\Events\FormAfterUpdate' => [
            'App\Listeners\NotifyAfterUpdate',
            'App\Listeners\WebhookAfterUpdate',
        ],
        'App\Events\FormBeforeDelete' => [
            'App\Listeners\WebhookBeforeDelete',
        ],
        'App\Events\FormAfterDelete' => [
            'App\Listeners\NotifyAfterDelete',
            'App\Listeners\WebhookAfterDelete',
        ],
        'App\Events\FormBeforeSave' => [
            'App\Listeners\WebhookBeforeSave',
        ],
        'App\Events\FormAfterSave' => [
            'App\Listeners\NotifyAfterSave',
            'App\Listeners\WebhookAfterSave',
        ],
    ];

    /**
     * The subscriber classes to register.
     *
     * @var array
     */
    protected $subscribe = [
        'App\Listeners\UserEventSubscriber',
    ];

    /**
     * Register any other events for your application.
     *
     * @return void
     */
    public function boot()
    {
        parent::boot();
    }
}

错误:

当我 运行 命令 php artisan event:generate 我得到以下错误:

PHP Fatal error:  Call to a member function listens() on null in /app/vendor/laravel/framework/src/Illuminate/Foundation/Console/EventGenerateCommand.php on line 35

                                                           
  [Symfony\Component\Debug\Exception\FatalErrorException]  
  Call to a member function listens() on null

根据文档,它应该这样做:

This command will generate any events or listeners that are listed in your EventServiceProvider. Events and listeners that already exist will be left untouched

我不明白我错过了什么,因为我在网上搜索没有发现任何类似的错误

这是返回的行 null:

$providers = $this->laravel->getProviders(EventServiceProvider::class);

因此,您的 EventServiceProvider 存在一些问题...请尝试使用此:

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Event;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        'App\Events\FormBeforeCreate' => [
            'App\Listeners\WebhookBeforeCreate',
        ],
        'App\Events\FormAfterCreate' => [
            'App\Listeners\NotifyAfterCreate',
            'App\Listeners\WebhookAfterCreate',
        ],
        'App\Events\FormBeforeUpdate' => [
            'App\Listeners\WebhookBeforeUpdate',
        ],
        'App\Events\FormAfterUpdate' => [
            'App\Listeners\NotifyAfterUpdate',
            'App\Listeners\WebhookAfterUpdate',
        ],
        'App\Events\FormBeforeDelete' => [
            'App\Listeners\WebhookBeforeDelete',
        ],
        'App\Events\FormAfterDelete' => [
            'App\Listeners\NotifyAfterDelete',
            'App\Listeners\WebhookAfterDelete',
        ],
        'App\Events\FormBeforeSave' => [
            'App\Listeners\WebhookBeforeSave',
        ],
        'App\Events\FormAfterSave' => [
            'App\Listeners\NotifyAfterSave',
            'App\Listeners\WebhookAfterSave',
        ],
    ];

    /**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        parent::boot();

        //
    }
}

不好意思,昨天晚上好像真的很累,我们的项目运行在Docker,我是运行在docker外面的命令,而不是在docker里面。

我不知道为什么它特别显示了这个错误,但是一旦我 运行 docker 中的命令正确生成所有文件。