dynamikaweb/yii2-factory-component

yii2 dynamically configurable construct

Installs: 516

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Type:yii2-extension

0.1 2021-05-26 16:57 UTC

This package is auto-updated.

Last update: 2024-04-09 01:44:41 UTC


README

php version pkg version license quality build

Installation

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.

How to use

Calling dynamically configured components

Wrong!

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

Correct!

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

Setup in PHP 7.4 or Higher

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

Setup in older´s PHP version

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

dynamika soluções web This project is under BSD-3-Clause license.