简体   繁体   中英

Laravel EventListener doesn't fire

I created an event and listener in Laravel, but the listener doesn't fire. It does actually fire on my colleague's machine. That makes me think that the actual code works and that the configuration is in order.

Listener:

<?php

namespace App\Listeners\Consensus;

use App\Events\Consensus\ManualGroupChannelNotificationEvent;
use Illuminate\Support\Facades\Log;

/**
 * Class ManualGroupChannelNotificationListener
 * @package App\Listeners\Consensus
 */
class ManualGroupChannelNotificationListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  ManualGroupChannelNotificationEvent  $event
     * @return void
     */
    public function handle(ManualGroupChannelNotificationEvent $event)
    {
        Log::debug('Listener');
    }
}

Event:

<?php
namespace App\Events\Consensus;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Support\Facades\Log;

/**
 * Class ManualGroupChannelNotificationEvent
 * @package App\Events\Consensus
 */
class ManualGroupChannelNotificationEvent
{
    use Dispatchable;

    /*
     * ExternalComment constructor.
     *
     * @param Comment $comment
     * @param User $currentUser
     */
    public function __construct()
    {
        Log::debug('Event');
    }
}

EventServiceProvider:

protected $listen = [
    'App\Events\Consensus\ManualGroupChannelNotificationEvent' => [
        'App\Listeners\Consensus\ManualGroupChannelNotificationListener',
    ],
];

Firing the event:

event(new ManualGroupChannelNotificationEvent());

I ran all commands to clear cache etc., but still it doesn't work.

php artisan clear-compiled
php artisan config:clear
php artisan cache:clear
composer dump-autoload
php artisan queue:restart

If this code works on another machine, what else can I do to make it work on mine?

Other info:

  • My logging does work; the Event-message is logged.
  • Other, similar events do work.

First thing, try to apply chmod -R 777 storage/logs it might be just an authorisation issue.

Maybe the registering of your events failed, you can try to add this to your EventServiceProvider

/**
 * Determine if events and listeners should be automatically discovered.
 *
 * @return bool
 */
public function shouldDiscoverEvents()
{
    return true;
}

Is your second working machine the same as the first one?

Ok. I tested your setup in Laravel 5.8 by manually creating your classes and both the event and listener are logged correctly.

I will suggest you avoid manually creating your event and listener classes. Instead, first specify them in EventServiceProvider.php as you have done:

protected $listen = [
    'App\Events\Consensus\ManualGroupChannelNotificationEvent' => [
        'App\Listeners\Consensus\ManualGroupChannelNotificationListener',
    ],
];

Then generate the necessary class files automatically using this artisan command

php artisan event:generate

You can now modify the generated classes by including the use Illuminate\Support\Facades\Log; directive line and then use the Log::debug() method call in your event constructor and listener handler methods.

Try this suggested method and see if it works. I don't think it is a case of storage access permission since the event logs successfully in your case.

I deleted the repo and cloned again, it works now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM