framework icon indicating copy to clipboard operation
framework copied to clipboard

No longer possible to programatically subscribe to events

Open adamjgriffith opened this issue 3 weeks ago • 3 comments

Laravel Version

11.x / 12.x

PHP Version

8.3.27

Database Driver & Version

Postgres

Description

In Laravel 10 and below it was possible to programatically subscribe to events like so:

<?php

namespace App\Listeners;

use App\Events\UserCreated;
use App\Events\UserUpdated;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;

class Subscriber implements ShouldQueue
{
    /**
     * Handle the event.
     *
     * @return void
     */
    public function handleUserCreated(UserCreated $event)
    {
        Log::info('Hello, world!');
    }

    /**
     * Handle the event.
     *
     * @return void
     */
    public function handleUserUpdated(UserUpdated $event)
    {
        Log::info('Hello, world!');
    }

    /**
     * Register the listeners for the subscriber.
     *
     * @param  \Illuminate\Events\Dispatcher  $events
     * @return void
     */
    public function subscribe($events)
    {
        if (config('app.env') === 'production') {
            $events->listen(
                'App\Events\UserCreated',
                [Subscriber::class, 'handleUserCreated']
            );

            $events->listen(
                'App\Events\UserUpdated',
                [Subscriber::class, 'handleUserUpdated']
            );
        }
    }
}

In Laravel 11 and 12 the listeners are fired regardless of the config('app.env') === 'production' check.

Steps To Reproduce

Copy example code from: https://laravel.com/docs/12.x/events#writing-event-subscribers.

Wrap the $events->listen calls in the subscribe method in any check even the if (false) {}.

Observe listeners are still fired.

adamjgriffith avatar Nov 19 '25 12:11 adamjgriffith