Php 使用数据库中的配置初始化应用程序组件

Php 使用数据库中的配置初始化应用程序组件,php,initialization,yii2,swiftmailer,Php,Initialization,Yii2,Swiftmailer,我正在构建一个Yii2应用程序,它通过电子邮件发送电子邮件。我将电子邮件设置(smtp、ssl、用户名等)存储在数据库表中,以便能够使用适当的视图对其进行编辑。 如何使用db表中的配置初始化swiftmailer 谢谢。您可以使用application objectYii::$app提供的方法初始化应用程序组件: use Yii; ... // Get config from db here Yii::$app->set('mailer', [ 'class' => '

我正在构建一个Yii2应用程序,它通过电子邮件发送电子邮件。我将电子邮件设置(smtp、ssl、用户名等)存储在数据库表中,以便能够使用适当的视图对其进行编辑。 如何使用db表中的配置初始化swiftmailer


谢谢。

您可以使用application object
Yii::$app
提供的方法初始化应用程序组件:

use Yii;

...

// Get config from db here

Yii::$app->set('mailer', [
    'class' => 'yii\swiftmailer\Mailer',
    'transport' => [
        'class' => 'Swift_SmtpTransport',
        // Values from db
        'host' => ... 
        'username' => ...
        'password' => ...
        'port' => ...
        'encryption' => ...
    ],
]);
然后像往常一样使用它:

use Yii;

...

Yii::$app->mailer->...
如果您希望对整个应用程序使用来自数据库的相同配置,则可以在应用程序引导期间获取并应用此配置

创建自定义类并将其放置在例如
app/components

namespace app\components;

use yii\base\BootstrapInterface;

class Bootstrap implements BootstrapInterface
{
    public function bootstrap($app)
    {
        // Put the code above here but replace Yii::$app with $app
    }
}
然后将其添加到配置中:

return [
    [
        'app\components\Bootstrap',
    ],
];
请注意:

如果已经存在具有相同ID的组件定义,则将 覆盖

正式文件:


    • 感谢和@arogachev的回答。这给了我一个想法,我解决了这个问题。我把这个贴出来是为了帮助任何人

      我解决了modyfing swiftmailer组件的问题,在Mailer.php中添加了以下内容:

      use app\models\Administracion; //The model i needed for access bd
       class Mailer extends BaseMailer
      {
      ...
      ...
      //this parameter is for the config (web.php)
      public $CustomMailerConfig = false;
      ...
      ...
      ...
      /**
           * Creates Swift mailer instance.
           * @return \Swift_Mailer mailer instance.
           */
          protected function createSwiftMailer()
          {
              if ($this->CustomMailerConfig) {
                  $model = new Administracion();
      
                  $this->setTransport([
                      'class' => 'Swift_SmtpTransport',
                      'host' => $model->getSmtpHost(),
                      'username' => $model->getSmtpUser(),
                      'password' => $model->getSmtpPass(),
                      'port' => $model->getSmtpPort(),
                      'encryption' => $model->getSmtpEncryption(),
                  ]);
              }
      
              return \Swift_Mailer::newInstance($this->getTransport());
          }
      
      'mailer' => [ 
                  'class' => 'yii\swiftmailer\Mailer',
                  'enableSwiftMailerLogging' =>true,
                  'CustomMailerConfig' => true, //if its true use the bd config else set the transport here
                  'useFileTransport' => false,
      ],
      
      在Web.php中添加了以下内容:

      use app\models\Administracion; //The model i needed for access bd
       class Mailer extends BaseMailer
      {
      ...
      ...
      //this parameter is for the config (web.php)
      public $CustomMailerConfig = false;
      ...
      ...
      ...
      /**
           * Creates Swift mailer instance.
           * @return \Swift_Mailer mailer instance.
           */
          protected function createSwiftMailer()
          {
              if ($this->CustomMailerConfig) {
                  $model = new Administracion();
      
                  $this->setTransport([
                      'class' => 'Swift_SmtpTransport',
                      'host' => $model->getSmtpHost(),
                      'username' => $model->getSmtpUser(),
                      'password' => $model->getSmtpPass(),
                      'port' => $model->getSmtpPort(),
                      'encryption' => $model->getSmtpEncryption(),
                  ]);
              }
      
              return \Swift_Mailer::newInstance($this->getTransport());
          }
      
      'mailer' => [ 
                  'class' => 'yii\swiftmailer\Mailer',
                  'enableSwiftMailerLogging' =>true,
                  'CustomMailerConfig' => true, //if its true use the bd config else set the transport here
                  'useFileTransport' => false,
      ],