我知道已经有一些线程,但似乎没有用...那:
(伪代码如下):
class GlobalVarClass{
globalVar $price;
function doSomethingWithThePrice(){
//get some Values, etc..
$this->price = $xyz;
}
function needThePriceNow(){
//this is where the price is needed now, so something like
echo $this->price;
}
}
我该怎么做?顺便说一句:我无法在构造函数中设置此值,因为我还没有它。...
我正在使用laravel btw。
编辑:确切地说,这是我拥有的代码:
class VoucherController extends Controller
{
private $_apiContext;
protected $voucherValue;
public function __construct()
{
$this->_apiContext = PayPal::ApiContext(
config('services.paypal.client_id'),
config('services.paypal.secret'));
$this->_apiContext->setConfig(array(
'mode' => 'sandbox',
'service.EndPoint' => 'https://api.sandbox.paypal.com',
'http.ConnectionTimeOut' => 30,
'log.LogEnabled' => true,
'log.FileName' => storage_path('logs/paypal.log'),
'log.LogLevel' => 'FINE'
));
}
public function index(){
return view('voucher.createVoucher');
}
public function payVoucher(Request $request){
$userInfo = AppUser::where('id','=',Auth::user()->id)
->first();
if($userInfo->street == ""){
$request->session()->flash('alert-info', 'Vor der ersten Bestellung müssen Sie erst Ihre Daten vervollständigen');
return redirect('/editData');
}
$itemList = PayPal::itemList();
$payPalItem = PayPal::item();
$payPalItem->setName('Gutschein')->setDescription('Gutschein für Shopping Portal')->setCurrency('EUR')->setQuantity(1)->setPrice($request->input('voucherValue'));
$total = $request->input('voucherValue');
$this->voucherValue = $total;
$itemList->addItem($payPalItem);
$payer = PayPal::Payer();
$payer->setPaymentMethod('paypal');
$details = PayPal::details();
$details->setShipping("0")
->setSubtotal($total);
$amount = PayPal:: Amount();
$amount->setCurrency('EUR');
$amount->setTotal(($total));
$amount->setDetails($details);
$transaction = PayPal::Transaction();
$transaction->setAmount($amount);
$transaction->setItemList($itemList);
$transaction->setDescription('Ihr Gutschein');
$redirectUrls = PayPal:: RedirectUrls();
$redirectUrls->setReturnUrl(action('VoucherController@getDone'));
$redirectUrls->setCancelUrl(action('VoucherController@getCancel'));
$payment = PayPal::Payment();
$payment->setIntent('sale');
$payment->setPayer($payer);
$payment->setRedirectUrls($redirectUrls);
$payment->setTransactions(array($transaction));
$response = $payment->create($this->_apiContext);
$redirectUrl = $response->links[1]->href;
return Redirect::to($redirectUrl);
}
public function getDone(Request $request)
{
$id = $request->get('paymentId');
$token = $request->get('token');
$payer_id = $request->get('PayerID');
$payment = PayPal::getById($id, $this->_apiContext);
$paymentExecution = PayPal::PaymentExecution();
$paymentExecution->setPayerId($payer_id);
$executePayment = $payment->execute($paymentExecution, $this->_apiContext);
// Clear the shopping cart, write to database, send notifications, etc.
// Thank the user for the purchase
//Gutscheincode generieren, in DB eintragen, und per Mail verschicken
$voucherCode = substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, 12);
$voucher = new Voucher;
$voucher->voucherValue = $this->voucherValue;
$voucher->voucherCode = $voucherCode;
$voucher->save();
return view('voucher.buyDone', ['voucherCode' => $voucherCode, 'payment' => $payment]);
}
所以我正在进行贝宝付款。在函数payVoucher
中,我从 request
中获得了凭证(应该在其他函数中可以访问的变量),然后在函数getDone
中,我需要访问它以将其输入到DB中。我该怎么做?
基于您的评论,第二种方法是在重定向后调用的。这是一个新请求,随着您在脚本中设置的所有变量在脚本完成后不再存在,您设置的属性将不再存在。
有几种方法可以将值传递给新请求,例如URL中的查询变量,会话变量或例如数据库存储。
使用会话或其他类型的服务器端存储保持服务器上的值,因此我建议这样做:
public function payVoucher(Request $request){
...
$_SESSION['PayPal']['voucherValue'] = $total;
...
}
public function getDone(Request $request)
{
...
$voucher->voucherValue = $_SESSION['PayPal']['voucherValue'];
// You need to delete it?
unset($_SESSION['PayPal']['voucherValue']);
...
}
请注意,我假设已经在脚本的顶部开始了会话。
尝试像这样
protected $price;
class GlobalVarClass{
public $price;
public function __construct(){
$this->price = 123;
}
function needThePriceNow(){
//this is where the price is needed now, so something like
echo $this->price; //This will give 123;
}
}