Php 设置amountToRelad var的值,以便可以在类上的任何方法上访问它
我在一个组件中工作,在工作中遇到一些问题。请看下面的代码:Php 设置amountToRelad var的值,以便可以在类上的任何方法上访问它,php,laravel,laravel-4,octobercms,Php,Laravel,Laravel 4,Octobercms,我在一个组件中工作,在工作中遇到一些问题。请看下面的代码: class Payment extends ComponentBase { /** * This hold the amount with PayPal fee and discount applied and pass back to template * @var float */ public $amountToReload; public function onAmount
class Payment extends ComponentBase
{
/**
* This hold the amount with PayPal fee and discount applied and pass back to template
* @var float
*/
public $amountToReload;
public function onAmountChange()
{
$amount = post('amount');
if (empty($amount)) {
throw new \Exception(sprintf('Por favor introduzca un valor.'));
}
$this->amountToReload = round($amount - ($amount * (float) Settings::get('ppal_fee') - (float) Settings::get('ppal_discount')), 2);
return ['#amountToReload' => $this->amountToReload];
}
public function onRun()
{
$step = $this->param('step');
$sandboxMode = Settings::get('sandbox_enabled');
switch ($step) {
case "step2":
echo $this->amountToReload;
$params = [
'username' => $sandboxMode ? Settings::get('ppal_api_username_sandbox') : Settings::get('ppal_api_username'),
'password' => $sandboxMode ? Settings::get('ppal_api_password_sandbox') : Settings::get('ppal_api_password'),
'signature' => $sandboxMode ? Settings::get('ppal_api_signature_sandbox') : Settings::get('ppal_api_signature'),
'testMode' => $sandboxMode,
'amount' => $this->amountToReload,
'cancelUrl' => 'www.xyz.com/returnUrl', // should point to returnUrl method on this class
'returnUrl' => 'www.xyz.com/cancelUrl', // should point to cancelUrl method on this class
'currency' => 'USD'
];
$response = Omnipay::purchase($params)->send();
if ($response->isSuccessful()) {
// payment was successful: update database
print_r($response);
} elseif ($response->isRedirect()) {
// redirect to offsite payment gateway
return $response->getRedirectResponse();
} else {
// payment failed: display message to customer
echo $response->getMessage();
}
break;
default:
break;
}
$this->page['step'] = $step;
}
public function cancelPayment()
{
// handle payment cancel
}
}
如果我将$amountoreload
作为公共声明的var加载到类的顶部,并在onAmountChange()
方法中为其设置值?然后在onRun()
方法中,这个变量不应该保留它的设置值?为什么它到达NULL或没有值?我是来自Symfony的新手。保持var值的最佳方法是什么,这样我就可以在整个类中毫无问题地使用它
作为本文的第二部分,我需要为cancelPayment()
方法生成一个有效的路由,这将在这一行进行:
'returnUrl' => 'www.xyz.com/cancelUrl', // should point to cancelUrl method on this class
如何在Laravel中创建一个可能带有参数的有效URL?使用URL帮助器?使用路线?哪一个?您的方法是好的,因为
amountToReload
被声明为类属性(尽管您可能希望将该属性设置为受保护的
,除非您明确希望将其公开)。唯一的问题是,在onRun()
之前需要调用方法onAmountChange()
,以便设置amountToReload
的值
至于生成url,最简单的方法是使用url()
:
有关更多信息,请查看。以及在
onRun()之前如何调用该方法。
?这背后的想法是一个向导,第一步是当输入值更改(
)时调用onAmountChange()
),所以想法是将计算出的数量传递给执行的代码onRun()
,否则我需要单独构建方法,我不知道如何做,有什么解决办法吗?我自己也没有用过OctoberCMS,我的答案是基于对你发布的代码的一般观察。在快速查看OctoberCMS文档之后,我觉得调用onAmountChange
方法是AJAX调用的结果,因此我认为它没有在与onRun
相同的上下文中被调用,这意味着不能真正使用class属性来传递值。因此,您必须将它与表单一起传递,以便onRun
方法可以访问它。这是我的推测,您应该更详细地阅读文档,以熟悉平台的功能。当在页面上呈现组件时,会自动调用onRun()方法。那么,当组件呈现在页面上时,您正在做一些特定的事情?@anandpatel我的想法就像一个向导,例如,在步骤1中,当输入更改其值时,我只执行函数onAmountChange
,为此,我使用了Ajax框架,然后,当用户单击步骤2时,我应该在swtich上执行第一个选项,正如您在我的代码中所看到的,也许我可以在wizStep2()
和wizStep3()
函数上拆分它,但使用这种方法,我不知道,首先如何在每个函数上传递或获取$amountToReload
值,然后如何执行它们,你清楚吗?
url('foo/bar', $parameters = array(), $secure = null);