1

I am trying to add multiple listeners on an event in Laravel 12 which is something like this:

AppServiceProvider.php

use App\Events\Frontend\OTPVerificationEvent;
use App\Listeners\Frontend\{OTPOnMobileListener, OTPOnWhatsappListener};

class AppServiceProvider extends ServiceProvider {
    ...

    /**
     * Bootstrap any application services.
     */
    public function boot(): void {
        Event::listen(OTPVerificationEvent::class, [
            [new OTPOnMobileListener, 'handle'],
            [new OTPOnWhatsappListener, 'handle'],
        ]);

        // --- OR ---

        Event::listen(OTPVerificationEvent::class, [
            new OTPOnMobileListener,
            new OTPOnWhatsappListener,
        ]);

        // --- OR ---

        Event::listen(OTPVerificationEvent::class, [
            OTPOnMobileListener::class,
            OTPOnWhatsappListener::class,
        ]);
    }
}

Events/Frontend/OTPVerificationEvent.php

class OTPVerificationEvent {
    /**
     * Public variables
     */
    public $mobile;
    public $mobileOtp;
    public $whatsappOtp;

    /**
     * Create a new event instance.
     */
    public function __construct($mobile, $mobileOtp, $whatsappOtp) {
        $this->mobile      = $mobile;
        $this->mobileOtp   = $mobileOtp;
        $this->whatsappOtp = $whatsappOtp;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return array<int, \Illuminate\Broadcasting\Channel>
     */
    public function broadcastOn(): array {
        return [
            // new PrivateChannel('channel-name'),
        ];
    }
}

Listeners/Frontend/OTPOnMobileListener.php

class OTPOnMobileListener {
    /**
     * Handle the event.
     */
    public function handle(object $event): void {
        echo "<pre>";
        print_r($event);
        echo "</pre>\n";
        exit;
    }
}

Listeners/Frontend/OTPOnWhatsappListener.php

class OTPOnWhatsappListener {
    /**
     * Handle the event.
     */
    public function handle(object $event): void {
        echo "<pre>";
        print_r($event);
        echo "</pre>\n";
        exit;
    }
}

I am keep getting this error:

First array member is not a valid class name or object

Error

5
  • 1
    Did you try to call Event::listen() multiple times? The documentation looks like Event::listen() only allows registering one listener per call. Commented Jul 2 at 7:33
  • Yes, but I found quite late about it. Honestly speaking, I prefer the older version of the code where you could provide an array defining multiple listeners for an event. This new code feels like repetitive as you have to initiate same event each time with new listener. Commented Jul 2 at 8:36
  • Fortunately, it doesn't get duplicated, because it chains the events into an array and associates both listeners with the event. So, as a result, Laravel merges the separately added listeners in the background. --> github.com/laravel/framework/blob/… Commented Jul 2 at 8:55
  • @Mr.Singh you can still make your own helper function that would take array of listeners and call Event::listen() for each of them. Commented Jul 2 at 8:58
  • Yes @MichalHynčica, I am aware of that. But, I prefer to use the things that come strait out of the box instead of adding additional code until it is absolutely necessary. Because it may introduce new complexities. Commented Jul 4 at 10:52

1 Answer 1

1
/*
  @method static void listen(
    \Illuminate\Events\QueuedClosure|callable|array|string $events,
    \Illuminate\Events\QueuedClosure|callable|array|string|null $listener = null
  )
*/
class Event extends Facade {/* ... */}

Although the listener method does accept an array as its second parameter, you would only use that approach if you need to specify a custom function name to be called (the default being handle). This is actually what you did in your first example, which works correctly in this case:

Event::listen(
    OTPVerificationEvent::class,
    [new OTPOnMobileListener, 'handle']
]);

Event::listen(
    OTPVerificationEvent::class,
    [new OTPOnWhatsappListener, 'handle']
]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.