The preferred way to install this extension is through composer.
Either run
$ composer require dynamikaweb/yii2-factory-component "*"
or add
"dynamikaweb/yii2-factory-component": "*"
to the require
section of your composer.json
file.
Yii::$app->components['mailer'];
Yii::$app->get('mailer');
common/configs/main.php
<?php
use dynamikaweb\fc\FactoryComponent as FC;
use common\models\ConfigModel;
return [
'components' => [
// other components ...
'mailer' => FC::build('yii\swiftmailer\Mailer', fn() => [
'viewPath' => '@common/mail',
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => ConfigModel::getInstance()->mail_host,
'username' => ConfigModel::getInstance()->mail_username,
'password' => ConfigModel::getInstance()->mail_password,
'port' => ConfigModel::getInstance()->mail_port,
'encryption' => ConfigModel::getInstance()->mail_encryption
],
'useFileTransport' => false,
]),
]
];
common/configs/main.php
<?php
use dynamikaweb\fc\FactoryComponent as FC;
use common\models\ConfigModel;
return [
'components' => [
// other components ...
'mailer' => FC::build('yii\swiftmailer\Mailer', function() {
$cfg = ConfigModel::getInstance();
return [
'viewPath' => '@common/mail',
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => $cfg->mail_host,
'username' => $cfg->mail_username,
'password' => $cfg->mail_password,
'port' => $cfg->mail_port,
'encryption' => $cfg->mail_encryption
],
'useFileTransport' => false,
];
}),
]
];
This project is under BSD-3-Clause license.