我确实从数据库中动态询问了组件中设置值的问题,为 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
],
最佳答案
如果您需要在运行时从数据库提供这些凭据,您可以使用 setComponents()
通过代码进行定义。 yii\base\Application
类的方法,您可以在其中从数据库中检索 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();
关于php - Yii2 - 在运行时附加组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56711056/