Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel EventListener doesn't fire

Tags:

php

laravel

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.
like image 333
Sherlock Avatar asked Oct 01 '19 14:10

Sherlock


2 Answers

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 ?

like image 125
Mathieu Ferre Avatar answered Sep 21 '22 21:09

Mathieu Ferre


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.

like image 35
Udo E. Avatar answered Sep 25 '22 21:09

Udo E.