我确实从数据库中动态询问了组件中设置值的问题,为 swiftmailer 提供了示例。同样得到了完美的回答here https://stackoverflow.com/questions/56708214/yii2-setting-value-in-config-from-component-or-database#56708214
但该答案仅适用于邮件程序组件,因此我如何实现类似的功能,例如,我需要在 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() https://www.yiiframework.com/doc/api/2.0/yii-di-servicelocator#setComponents()-detail的方法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();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)