-
Notifications
You must be signed in to change notification settings - Fork 11.7k
Open
Description
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
Labels
No labels