使用数据库 [Laravel] 中的值进行动态邮件配置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45146260/
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
Dynamic mail configuration with values from database [Laravel]
提问by Saurabh
I have created a service provider in my Laravel Application SettingsServiceProvider
. This caches the settings table from the database.
我在我的 Laravel 应用程序中创建了一个服务提供者SettingsServiceProvider
。这会缓存数据库中的设置表。
$settings = $cache->remember('settings', 60, function() use ($settings)
{
return $settings->pluck('value', 'name')->all();
});
config()->set('settings', $settings);
settings
table:
settings
桌子:
I am able to echo the value from the table like this:
我可以像这样从表中回显值:
{{ config('settings.sitename') }} //returns Awesome Images
This works fine on any blade files or controllers in App\Http\Controllers
这适用于任何刀片文件或控制器 App\Http\Controllers
Problem:
问题:
I am trying to echo the value to App\config\mail.php
like this:
我试图回应这样的价值App\config\mail.php
:
'driver' => config('settings.maildriver'),
'host' => config('settings.mailhost'),
But I'm getting this error:
但我收到此错误:
Missing argument 1 for Illuminate\Support\Manager::createDriver()
Update:
更新:
I have created a new service provider MailServiceProvider
to override the settings in Mail.php like this:
我创建了一个新的服务提供者MailServiceProvider
来覆盖 Mail.php 中的设置,如下所示:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Config;
class MailServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
Config::set('mail.driver', config('settings.maildriver'));
Config::set('mail.host', config('settings.mailhost'));
Config::set('mail.port', config('settings.mailport'));
Config::set('mail.encryption', config('settings.mailencryption'));
Config::set('mail.username', config('settings.mailusername'));
Config::set('mail.password', config('settings.mailpassword'));
}
}
But still I am getting the same error!!
但我仍然遇到同样的错误!!
Is there any way to override default mail configuration (in app/config/mail.php
) on-the-fly (e.g. configuration is stored in database) before swiftmailer transport is created?
app/config/mail.php
在创建 swiftmailer 传输之前,有什么方法可以即时覆盖默认邮件配置(在)(例如,配置存储在数据库中)?
回答by Saurabh
Struggled for 3 days with this issue finally I figured out a way to solve it.
在这个问题上挣扎了 3 天,我终于找到了解决它的方法。
First I created a table mails
and populated it with my values.
Then I created a provider MailConfigServiceProvider.php
首先,我创建了一个表mails
并用我的值填充它。然后我创建了一个提供者MailConfigServiceProvider.php
<?php
namespace App\Providers;
use Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;
class MailConfigServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
if (\Schema::hasTable('mails')) {
$mail = DB::table('mails')->first();
if ($mail) //checking if table is not empty
{
$config = array(
'driver' => $mail->driver,
'host' => $mail->host,
'port' => $mail->port,
'from' => array('address' => $mail->from_address, 'name' => $mail->from_name),
'encryption' => $mail->encryption,
'username' => $mail->username,
'password' => $mail->password,
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
);
Config::set('mail', $config);
}
}
}
}
And then registered it in the config\app.php
然后注册到 config\app.php
App\Providers\MailConfigServiceProvider::class,
回答by Ramon Bakker
Maybe its usefull to somebody, but I solved it as following;
也许它对某人有用,但我解决了以下问题;
In a ServiceProvider
under the boot
-method;
在一个ServiceProvider
下boot
-method;
$settings = Cache::remember('settings', 60, function () {
return Setting::pluck('value', 'name')->all();
});
config()->set('settings', $settings); // optional
config()->set('mail', array_merge(config('mail'), [
'driver' => 'mailgun',
'from' => [
'address' => $settings['mail_from_address'],
'name' => $settings['mail_from_name']
]
]));
config()->set('services', array_merge(config('services'), [
'mailgun' => [
'domain' => $settings['mailgun_domain'],
'secret' => $settings['mailgun_secret']
]
]));
I used array_merge
with the original config, so we only override the values we need to. Also we can use the Cache
-facade in the boot
-method.
我使用array_merge
了原始配置,所以我们只覆盖了我们需要的值。我们也可以Cache
在boot
-method 中使用-facade 。