You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Related to #19457
The current behavior of loading app.providers after the auto discovered providers is causing some conflicts with other Laravel features (model observers)
Steps To Reproduce:
On a clean and fresh Laravel 5.5 installation..
Create a new package that has the following:
a. A Model
b. A Model Observer
c. Auto-Discovered Service Provider
Now as per Laravel's docs we'll register the model observer in the package's Auto-Discovered service provider's boot method.
Try now to do any action that trigger any observed model events, and you'll be surprised that nothing will happen. The model observer is useless and here's why in a pseudo workflow: PackageServiceProvider::boot() -> Model::observe() -> Model::registerModelEvent() -> isset(static::$dispatcher) -> static::$dispatcher == null tada!
Why static::$dispatcher is null, because the Auto-Discovered package's service provider, that register the model observer, which requires the event dispatcher, is registered first before Laravel's Illuminate\Database\DatabaseServiceProvider where the event dispatcher is being injected to the model through Model::setEventDispatcher. So in short, the model listeners never registered!
This is a clean and simple implementation of few basic Laravel features, that doesn't override any core functionality, and I can't see any reason not to use the Auto-Discovery feature. What do you think, any thoughts..? @taylorotwell@driesvints
Suggested solution
Load app.providers before the auto discovered providers.
The text was updated successfully, but these errors were encountered:
Description:
Related to #19457
The current behavior of loading app.providers after the auto discovered providers is causing some conflicts with other Laravel features (model observers)
Steps To Reproduce:
On a clean and fresh Laravel 5.5 installation..
a. A Model
b. A Model Observer
c. Auto-Discovered Service Provider
boot
method.PackageServiceProvider::boot() -> Model::observe() -> Model::registerModelEvent() -> isset(static::$dispatcher) -> static::$dispatcher == null tada!
Why
static::$dispatcher
is null, because the Auto-Discovered package's service provider, that register the model observer, which requires the event dispatcher, is registered first before Laravel'sIlluminate\Database\DatabaseServiceProvider
where the event dispatcher is being injected to the model throughModel::setEventDispatcher
. So in short, the model listeners never registered!This is a clean and simple implementation of few basic Laravel features, that doesn't override any core functionality, and I can't see any reason not to use the Auto-Discovery feature. What do you think, any thoughts..? @taylorotwell @driesvints
Suggested solution
Load app.providers before the auto discovered providers.
The text was updated successfully, but these errors were encountered: