laravel 在运行时更改服务配置参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38873970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:17:26  来源:igfitidea点击:

Change service config parameters at runtime

laravellaravel-5laravel-5.2

提问by Tiago Gouvêa

I'm using mailgun to send mails thought Laravel 5.2. It configured on config/services.php like that:

我正在使用 mailgun 发送邮件,认为 Laravel 5.2。它在 config/services.php 上这样配置:

    'mailgun' => [
        'domain' => env('mailgun_domain','mydomain.com'),
        'secret' => env('mailgin_secret','my-secret-key-132152345423')
    ],

But, I need change that settings in run time, before call Mail::send, to use the correct service parameters. It must be changed many times during runtime.

但是,在调用 Mail::send 之前,我需要在运行时更改该设置,以使用正确的服务参数。它必须在运行时多次更改。

I cannot configure it by .env file, because all data will be get from database, where the user setups the domain and secret.

无法通过 .env 文件配置它,因为所有数据都将从数据库中获取,用户在其中设置域和机密。

回答by Alexey Mezenin

You can set config values dynamically at runtime with config()helper:

您可以在运行时使用config()helper动态设置配置值:

config(['services.mailgun' => $arrayWithNewSettings]);

回答by Jared Eitnier

I think if you follow this exampleyou'll be on the right track.

我认为如果你遵循这个例子,你就会走上正轨。

\Illuminate\Mail\TransportManager.phphas a method createMailgunDriver()which is pulling services from a hard location that is not changeable by default.

\Illuminate\Mail\TransportManager.php有一种方法createMailgunDriver()可以从默认情况下不可更改的硬位置提取服务。

You'll need to write your own service provider, extend the MailServiceProviderand roll your own transport so that you can pull your settings from the db like you want to.

您需要编写自己的服务提供程序,扩展MailServiceProvider并滚动您自己的传输,以便您可以根据需要从数据库中提取设置。