Boot order issue in LdapServiceProvider #425
-
I have made a library that uses the LdapRecord DirectoryEmulator as its own service provider. Upon attempting to use my library I found that it was throwing the classic It was working in my library, and after some investigation I found that my library was booting before LdapRecord. Normally this wouldn't be an issue, however I found that the LdapServiceProvider only uses the I have a workaround for my issue by removing my service provider's autoloader and registering it manually, thus forcing the right boot order. Modifying the LdapServiceProvider to add the public function register(): void
{
$this->registerLdapConnections();
if ($this->app->runningUnitTests()) {
$this->app->singleton(LdapDatabaseManager::class);
}
}
public function boot(): void
{
$this->loadEnvironmentConnections();
$this->registerLogging();
$this->registerConfiguration();
$this->registerCommands();
} It seems like there may be a lot of things being booted that should be registered instead. Why isn't the LdapServiceProvider using |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @AnthonyEdmonds! This is because Laravel's service provider https://laravel.com/docs/9.x/providers#the-register-method When Laravel calls the Here is an example of Laravel Cashier performing the same order of operations: https://github.com/laravel/cashier-stripe/blob/13.x/src/CashierServiceProvider.php If you would like to change the boot order, you can disable auto-discovery of the 'providers' => [
// ...
\LdapRecord\Laravel\LdapServiceProvider::class,
\App\Providers\MyAppServiceProvider::class,
] |
Beta Was this translation helpful? Give feedback.
Hi @AnthonyEdmonds!
This is because Laravel's service provider
register()
method should only be used to bind things into the service container:https://laravel.com/docs/9.x/providers#the-register-method
When Laravel calls the
register()
method in a service provider, it is not guaranteed that all of the other application service providers have in-fact registered. Meaning that the configuration, eloquent, authentication, and other service provides may not be ready to be used. LdapRecord-Laravel uses these services when registering connections (requiring theConfiguration
service provider to be loaded).Here is an example of Laravel Cashier performing the same order of operations:
https://gi…