Change service config parameters at runtime

时光怂恿深爱的人放手 提交于 2020-06-27 06:41:17

问题


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

    '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.

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


回答1:


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

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



回答2:


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

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

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



来源:https://stackoverflow.com/questions/38873970/change-service-config-parameters-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!