使用数据库中的值进行动态邮件配置[Laravel] [英] Dynamic mail configuration with values from database [Laravel]
问题描述
我已经在我的Laravel应用程序SettingsServiceProvider
中创建了一个服务提供者.这将从数据库中缓存设置表.
I have created a service provider in my Laravel Application SettingsServiceProvider
. This caches the settings table from the database.
$settings = $cache->remember('settings', 60, function() use ($settings)
{
return $settings->pluck('value', 'name')->all();
});
config()->set('settings', $settings);
settings
表:
我能够像这样从表中回显值:
I am able to echo the value from the table like this:
{{ config('settings.sitename') }} //returns Awesome Images
这在App\Http\Controllers
问题:
我试图像这样将值回显到App\config\mail.php
:
I am trying to echo the value to App\config\mail.php
like this:
'driver' => config('settings.maildriver'),
'host' => config('settings.mailhost'),
但是我遇到了这个错误:
But I'm getting this error:
Missing argument 1 for Illuminate\Support\Manager::createDriver()
更新:
我创建了一个新的服务提供商MailServiceProvider
,以覆盖Mail.php中的设置,如下所示:
I have created a new service provider MailServiceProvider
to override the settings in Mail.php like this:
<?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!!
在创建swiftmailer传输之前,是否有任何方法可以即时覆盖默认邮件配置(例如,在app/config/mail.php
中)(例如,配置存储在数据库中)?
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?
推荐答案
为此问题苦苦挣扎了3天,我终于找到了解决之道.
Struggled for 3 days with this issue finally I figured out a way to solve it.
首先,我创建了一个表mails
,并用我的值填充了该表.
然后,我创建了一个提供程序MailConfigServiceProvider.php
First I created a table mails
and populated it with my values.
Then I created a provider 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);
}
}
}
}
然后将其注册到config\app.php
App\Providers\MailConfigServiceProvider::class,
这篇关于使用数据库中的值进行动态邮件配置[Laravel]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!