Skip to content

No longer possible to programatically subscribe to events #57818

@adamjgriffith

Description

@adamjgriffith

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions