访问OctoberCMS中组件的设置

时间:2014-10-14 14:10:52

标签: php laravel 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')空了,它没有用,为什么?我做错了什么?

1 个答案:

答案 0 :(得分:1)

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

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-settings