Yii2-在运行时附加组件

时间:2019-06-21 22:23:32

标签: php yii2

我确实从数据库中动态询问了组件中一个设置值的问题,为swiftmailer提供了示例。完美的答案here

但是该答案仅适用于邮件组件,因此,例如,如何实现类似功能,我需要在config.php中添加以下值:

'pp' => [ 
    'class' => 'app/components/paypal', // note: this has to correspond with the newly created folder, else you'd get a ReflectionError

     // Next up, we set the public parameters of the class
    'client_id' => 'YOUR-CLIENT-ID-FROM-PAYPAL',
    'client_secret' => 'YOUR-CLIENT-SECRET-FROM-PAYPAL',
    // You may choose to include other configuration options from PayPal
    // as they have specified in the documentation
  ],

1 个答案:

答案 0 :(得分:1)

如果您需要在运行时从数据库中提供这些凭据,则可以使用yii\base\Application类的setComponents()方法通过代码定义代码,在该方法中,您将从数据库中获取Paypal和从配置文件中删除它。

添加以下行以在运行时设置组件,然后调用所需的方法

Yii::$app->setComponents(
    [
        'pp' => [
            'class' => 'app/components/paypal', // note: this has to correspond with the newly created folder, else you'd get a ReflectionError

            // Next up, we set the public parameters of the class
            'client_id' => 'YOUR-CLIENT-ID-FROM-PAYPAL',
            'client_secret' => 'YOUR-CLIENT-SECRET-FROM-PAYPAL'
            // You may choose to include other configuration options from PayPal
            // as they have specified in the documentation
        ]
    ]
);

//now you can call the desired method for the pp with the above credentials
Yii::$app->pp->checkout();
相关问题