从OctoberCMS中的组件访问设置


Access settings from component in OctoberCMS

正如标题所说,我正在使用插件中的设置模型访问组件的设置,但得到的结果是空白的,而不是存储在DB中的当前值。这就是我正在做的。首先,这是我的fields.yaml文件定义:

fields:
    sandbox_enabled:
      label: Activar modo Sandbox?
      type: checkbox
      default: false
    ppal_fee:
      label: Cargo de PayPal
      type: text
    ppal_discount:
      label: Descuento por transferencias
      type: text
    ppal_app_id_sandbox:
      label: PayPal Sandbox App ID (*)
      type: text
    ppal_developer_account:
      label: PayPal Developer Account Email Address (*)
      type: text
    ppal_api_username_sandbox:
      label: PayPal Username API Credentials (Sandbox) (*)
      type: text
    ppal_api_password_sandbox:
      label: PayPal Password API Credentials (Sandbox) (*)
      type: text
    ppal_api_signature_sandbox:
      label: PayPal Signature API Credentials (Sandbox) (*)
      type: text
    ppal_rest_client_id_sandbox:
      label: PayPal REST API Username Credentials (Sandbox) (*)
      type: text
    ppal_rest_client_secret_sandbox:
      label: PayPal REST API Secret Credentials (Sandbox) (*)
      type: text

第二,alomicuba.recharge.settings在DB:记录值

{
     "sandbox_enabled":"1",
     "ppal_fee":"0.029",
     "ppal_discount":"0.30",
     "ppal_app_id_sandbox":"",
     "ppal_developer_account":"",
     "ppal_api_username_sandbox":"",
     "ppal_api_password_sandbox":"",
     "ppal_api_signature_sandbox":"",
     "ppal_rest_client_id_sandbox":"",
     "ppal_rest_client_secret_sandbox":""
  }

最后,我是如何获得组件中设置的值的:

public function onAmountChange()
{
    // The user amount
    $amount = post('amount');
    if (empty($amount))
    {
        throw new 'Exception(sprintf('Por favor introduzca un valor.'));
    }
    $toReload = $amount - ($amount * Settings::get('ppal_fee') - Settings::get('ppal_discount'));
    $this->page["confirmation_text"] = $toReload;
}

但它不起作用,因为这两个值Settings::get('ppal_fee')Settings::get('ppal_discount')为空,为什么?我做错了什么?

您在顶部指定了以下代码行吗?

use Alomicuba'Recharge'Models'Settings;

如果您已经指定,然后尝试使用以下代码行:

$settings = Settings::instance();
$settings->ppal_fee;

所有的功能代码看起来都像这个

public function onAmountChange()
{
    // The user amount
    $amount = post('amount');
    if (empty($amount))
    {
        throw new 'Exception(sprintf('Por favor introduzca un valor.'));
    }
    $settings = Settings::instance();
    $toReload = $amount - ($amount * $settings->ppal_fee - $settings->ppal_discount);
    $this->page["confirmation_text"] = $toReload;
}

参考https://octobercms.com/docs/plugin/settings#reading-设置