根据另一个服务配置一个服务

Configuring a service according to another service

我有一个名为 setting 的服务,它从某处获取用户的偏好。我需要在配置其他一些服务时使用它。例如,我需要获取用户的电子邮件地址电子邮件主机。

config/mail.php 文件中使用 'host' => app('setting')['email']['host']),, 不起作用。似乎在那个阶段无法访问 app() 服务。 正确的方法是什么?

您可以在运行时在服务提供商内更改配置。

我们只使用 Laravel 附带的 app/Providers/AppServiceProvider。因此,在 boot() 方法中,您可以像这样对配置进行任何更改:

public function boot()
{
    $this->app->make('config')->set('whatever.config', 'This has changed');
}