php - Yii2 - 在运行时附加组件

标签 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
  ],

最佳答案

如果您需要在运行时从数据库提供这些凭据,您可以使用 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/

相关文章:

javascript - 如何从 PHP 获取 deflate 数据到 Javascript

php - 如何将 php 下拉菜单链接到其他 php 并显示 mysql 数据?

javascript - 在 View yii2 中使用内联脚本

datepicker - Yii2 Kartik DatePicker - 日期不应超过当前日期

php - 验证 opencart 中新添加的注册表单

php - 将 mysql echo 作为 ajax 中的错误返回

php - php 中的 AES 加密和 perl 中的解密

php - 文档级读取权限联结表?

url - SEO 和页面地址的更改

gridview - 如何在 yii2 分页中使用 bootstrap glyphicon 图标作为上一个和下一个按钮?