如何在Laravel中使用数据库进行邮件设置 [英] How to use database for mail settings in Laravel
问题描述
我想避免用户编辑配置文件,因此我在管理面板中设置了Web界面,用于设置邮件服务器,用户名,密码,端口,加密. 我在Laravel 4.2中运行良好,但是现在将应用程序重写为Laravel 5时,会发生错误:
I'd like to keep users away from editing configuration files, so I've made web interface in admin panel for setting up Mail server, username, password, port, encryption.. I was working well in Laravel 4.2, but now when the app has been rewritten into Laravel 5, an error occurs:
Class 'Settings' not found in <b>F:\htdocs\app\config\mail.php</b> on line <b>18</b><br />
为此,我创建了一个服务提供程序,制作了一个外观,将它们放在config/app.php中,Settings::get('var')/Settings::set('var')
可以完美地工作,但不适用于邮件设置.
For this purpose I've created a service provider, made a facade, put them in config/app.php, Settings::get('var')/Settings::set('var')
work perfectly, but not for mail settings.
config/mail.php:
config/mail.php:
<?php return array(
'driver' => Settings::get('mail_driver'),
'host' => Settings::get('mail_host'),
'port' => Settings::get('mail_port'),
'from' => array('address' => Settings::get('mail_from_address'), 'name' => Settings::get('mail_from_name')),
'encryption' => Settings::get('mail_encryption'),
'username' => Settings::get('mail_username'),
'password' => Settings::get('mail_password'),
'sendmail' => Settings::get('mail_sendmail'),
'pretend' => false,
);
config/app.php:
config/app.php:
'providers' => [
...
'App\Providers\SettingsServiceProvider',
...
'aliases' => [
...
'Settings' => 'App\Custom\Facades\Settings',
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Custom\Settings;
class SettingsServiceProvider extends ServiceProvider {
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->singleton('settings', function()
{
return new Settings;
});
}
}
<?php namespace App\Custom;
use App\Setting;
class Settings {
public function get($var) {
try{
$setting = Setting::first();
} catch(exception $e)
{
return false;
}
return $setting->$var;
}
public function set($var, $val) {
try{
$setting = Setting::first();
$setting->$var = $val;
$setting->save();
} catch(exception $e)
{
return false;
}
return true;
}
}
<?php
namespace App\Custom\Facades;
use Illuminate\Support\Facades\Facade;
class Settings extends Facade {
protected static function getFacadeAccessor() { return 'settings'; }
}
有什么想法如何使用数据库来实现Laravel邮件设置吗?
Any ideas how to implement Laravel mail settings using database?
推荐答案
要对此进行存档,我通过扩展Illuminate\Mail\MailServiceProvider
创建了CustomMailServiceProvider
以便覆盖此方法:
To archive this I created CustomMailServiceProvider
by extending Illuminate\Mail\MailServiceProvider
so as to overwrite this method:
protected function registerSwiftTransport(){
$this->app['swift.transport'] = $this->app->share(function($app)
{
return new TransportManager($app);
});
}
这是完整的解决方案
- 我在app \ Providers中创建了CustomMailServiceProvider.php
namespace App\Providers;
use Illuminate\Mail\MailServiceProvider;
use App\Customs\CustomTransportManager;
class CustomMailServiceProvider extends MailServiceProvider{
protected function registerSwiftTransport(){
$this->app['swift.transport'] = $this->app->share(function($app)
{
return new CustomTransportManager($app);
});
}
}
- 我在app/customs目录中创建了CustomTransportManager.php- 注意:app/customs目录在默认的laravel 5目录结构中不存在,我创建了它
- I created CustomTransportManager.php in app/customs directory - NB: app/customs directory doesn't exist in default laravel 5 directory structure, I created it
namespace App\Customs;
use Illuminate\Mail\TransportManager;
use App\Models\Setting; //my models are located in app\models
class CustomTransportManager extends TransportManager {
/**
* Create a new manager instance.
*
* @param \Illuminate\Foundation\Application $app
* @return void
*/
public function __construct($app)
{
$this->app = $app;
if( $settings = Setting::all() ){
$this->app['config']['mail'] = [
'driver' => $settings->mail_driver,
'host' => $settings->mail_host,
'port' => $settings->mail_port,
'from' => [
'address' => $settings->mail_from_address,
'name' => $settings->mail_from_name
],
'encryption' => $settings->mail_encryption,
'username' => $settings->mail_username,
'password' => $settings->mail_password,
'sendmail' => $settings->mail_sendmail,
'pretend' => $settings->mail_pretend
];
}
}
}
- 最后,我用
'App\Providers\CustomMailServiceProvider',
替换了config/app.php中的
'Illuminate\Mail\MailServiceProvider',
这篇关于如何在Laravel中使用数据库进行邮件设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!