Skip to content

Streamline EventServiceProvider into RequestLoggerServiceProvider #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 11, 2025

Conversation

onlime
Copy link
Contributor

@onlime onlime commented Feb 10, 2025

The EventServiceProvider (which extended Illuminate\Foundation\Support\Providers\EventServiceProvider) in this package gave my a huge headache: The framework's Register event was always triggered twice because of this double registration in EventServiceProvider:

class EventServiceProvider extends ServiceProvider
{
    // ...
    public function register()
    {
        // ...
        $this->booted(function () {
            $this->configureEmailVerification();
        });
    }

    protected function configureEmailVerification()
    {
        if (! isset($this->listen[Registered::class]) ||
            ! in_array(SendEmailVerificationNotification::class, Arr::wrap($this->listen[Registered::class]))) {
            Event::listen(Registered::class, SendEmailVerificationNotification::class);
        }
    }

On any user registration, the SendEmailVerificationNotification listener got triggered twice and the user effectively got two verification emails.

I stepped into this problem previously on my own package onlime/laravel-http-client-global-logger where @pascalbaljet and I fixed it in v1.1.3 by ditching the whole EventServiceProvider.

This bug is extremely hard to track down if you're not into the framework. Even the debug_backtrace() of SendEmailVerificationNotification would lead to the same backtrace, and the Register event only gets dispatched in Laravel's RegisteredUserController.

The problem only seems to occur since Laravel 11, related to the introduced Email Verification Notification on Registration SendEmailVerificationNotification auto-registration.

Possibly related issues of others struggling with it:

So, in this PR I have just streamlined EventServiceProvider into RequestLoggerServiceProvider by this simple listener registration:

Event::listen(RequestHandled::class, LogRequest::class);

which fixes all the negative side-effects of duplicate EventServiceProvider registration.

@bilfeldt
Copy link
Owner

Thanks so much for this PR @onlime

Your explanation seems very plausible and the fix makes a lot of sense to me. The test LogRequestListenerTest ensures that this should not cause any problems which is great.

@bilfeldt bilfeldt merged commit 1da567d into bilfeldt:main Feb 11, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants