PHP code example of dynamikaweb / yii2-factory-component

1. Go to this page and download the library: Download dynamikaweb/yii2-factory-component library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dynamikaweb / yii2-factory-component example snippets


Yii::$app->components['mailer'];

Yii::$app->get('mailer');



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,
        ]),
    ]
];



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,
            ];
        }),
    ]
];