Skip to content
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

Register the DB listener through the boot function instead of register function #37

Open
zyz954489346 opened this issue Feb 2, 2024 · 1 comment

Comments

@zyz954489346
Copy link

zyz954489346 commented Feb 2, 2024

All service providers extend the Illuminate\Support\ServiceProvider class. Most service providers contain a register and a boot method. Within the register method, you should only bind things into the service container. You should never attempt to register any event listeners, routes, or any other piece of functionality within the register method.

As stated in the laravel document.The registration of listener should not be placed on register. This is the current provider code:

    public function register()    
    {    
        // ...    
        // listen to database queries    
        $this->app['db']->listen($this->getListenClosure($logger));    
    }    

$this->app['db'] will cause db to be instantiated,Any provider executed after that, if it contains changes to db, will be ignore because the db is instantiated in advance.

There is a bug, which causes the official package of laravel-mongodb not to be correctly register in the resolving callback of db. This bug is modified by the official suggestion of mongodb.
see mongodb/laravel-mongodb#2715 (comment)

So the solution to the problem is that db should not be instantiated in register, which does not conform to the specification of laravel and can lead to conflicts between packages.

@r0bdiabl0
Copy link

This caused me a headache for 3 days. I had to slowly remove packages from composer.json to discover this package was the issue. I use Laravel 11 and MongoDB Laravel and this package would prevent me from adding MongoDB as a new connection. Please update!

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

No branches or pull requests

2 participants