Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kitten's avatar

Event Listeners

Hello!

I have a question because I couldn't find an answer anywhere.. Is there a way to bind Event listeners to an event not mapped to the EventServiceProvider from the laravel framework?

For example, I have a theme so I would rather not modify any of the core (Laravel's framework) files, instead I would like to have my Events and Listeners in a specific directory inside the theme. Not modifying the Laravel's files would make it easier for me to move the theme on another Laravel setup without headaches.

My event resides in app/Events/MyEvent.php, and the listener in app/Listeners/MyEventListener.php.

In my code, I have this:

if ( $currentUser->id != $visitedUser->id ) {
	event( new MyEvent( $currentUser, $visitedUser ) );
}

but the Event listener is never triggered. Is there a way to make this work with having to modify the Laravel's EventServiceProvider.php class?

TIA

0 likes
2 replies
kitten's avatar

ha.. I've found the answer in https://laravelactions.com/

Looks like this did what I needed:

Event::listen(MyEvent::class, myListener::class);

woohoo! I am so happy!

Please or to participate in this conversation.