问题
I was fetch mail configuration data using model file, and pass it to my provider the name CofigServiceProvider.php. But i don't know how to pass it into config/mail.php file. Because i need to change the mail configuration data dynamically from my admin panel. or Any other way to fetch db values and pass it app/config/mail.php in laravel 4
ConfigServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Config;
use Home;
class ConfigServiceProvider extends ServiceProvider {
public function register() {
$smtpdata = Home::get_smtp_data();
Config::set('database.connections.mysql.host', $smtpdata->sm_host);
Config::set('database.connections.mysql.port', $smtpdata->sm_port);
Config::set('database.connections.mysql.username', $smtpdata->sm_uname);
Config::set('database.connections.mysql.password', $smtpdata->sm_pwd);
}
}
Home.php(model)
public static function get_smtp_data()
{
return DB::table('nm_smtp')->where('sm_isactive','=', 1)->first();
}
config/Mail.php:
<?php
return array(
'driver' => 'smtp',
'host' => 'smtp.gmail.com',
'port' => 465,
/*
'from' => array('address' => null, 'name' => null),
===> */
'from' => array('address' => 'myaccount@gmail.com', 'name' => 'Laravel'),
'encryption' => 'ssl',
'username' => 'myname@gmail.com',
'password' => '******',
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
);
回答1:
Jus update your ConfigServiceProvider like,
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Config;
use Home;
class ConfigServiceProvider extends ServiceProvider {
public function register() {
$smtpdata = Home::get_smtp_data();
Config::set('mail.host', $smtpdata->sm_host);
Config::set('mail.port', $smtpdata->sm_port);
Config::set('mail.username', $smtpdata->sm_uname);
Config::set('mail.password', $smtpdata->sm_pwd);
}
}
来源:https://stackoverflow.com/questions/37804768/how-to-pass-mail-data-from-service-provider-to-config-mail-php-in-laravel-4