Antonio Milat
Antonio Milat

Reputation: 11

Why is my Laravel Lumen listener being called twice?

My listener is being called twice, but the event is fired only once.

I got the code from Lumen 5.7 example and have no idea where is my mistake.

bootstrap/app.php

$app->register(App\Providers\EventServiceProvider::class);

app/Providers/EventServiceProvider.php

protected $listen = [
    'App\Events\NewAuthEvent' => [
        'App\Listeners\SendNewAuthListener',
    ],
];

app/Events/NewAuthEvent.php

use Illuminate\Queue\SerializesModels;
use App\Data\Entity\User;
use App\Data\Entity\Authorization;
use Illuminate\Support\Facades\Log;

class NewAuthEvent
{
    use SerializesModels;

    public $user;

    public $auth;

    public function __construct(User $user, Authorization $auth)
    {
        Log::debug("once :)");
        $this->user = $user;
        $this->auth = $auth;
    }
}

app/Listeners/SendNewAuthListener.php

use App\Events\NewAuthEvent;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Data\Service\MessengerService;
use Illuminate\Support\Facades\Log;

class SendNewAuthListener
{
    private $messenger;

    public function __construct(MessengerService $messenger)
    {
        Log::debug("twice :(");
        $this->messenger = $messenger;
    }

    public function handle(NewAuthEvent $event)
    {
        Log::debug("twice as well :(");
        $this->messenger->new($event->user, $event->auth);
    }
}

firing the event:

event(new NewAuthEvent($objUser, $objAuthorization));

What am I missing? Is there some other parameter or config to be seted?

Thanks in advance!

Upvotes: 0

Views: 1956

Answers (1)

Chris Mayers
Chris Mayers

Reputation: 31

I had the same issue. Check your EventsServiceProvider. If your are using a shouldDiscoverEvents method that returns true, removing it may solve your issue. See comment by andnacho here.

Upvotes: 3

Related Questions