尝试在脚本中包含 php 文件,即使页面加载时没有显示或记录错误,也不会执行包含的文件。
我创建了一个如下所示的测试文件:
<?php
$sale_amount = '10.00';
$product = 'Drive-Plan';
include('partners/controller/record-sale.php');
echo "commission counted"
?>
在浏览器中打开此文件将完成预期的功能。
尝试在我的脚本中包含相同的代码并不能完成该函数。
原始脚本效果很好:
/**
* Swap current users plan to a new one.
*
* @return 'Illuminate'Contracts'Routing'ResponseFactory|'Symfony'Component'HttpFoundation'Response
*/
public function swapPlan() {
if ($this->user->subscribed() && Input::get('plan')) {
$this->user->subscription(Input::get('plan'))->swap();
return response(trans('app.planSwapSuccess', ['plan' => Input::get('plan')]), 200);
}
}
在此脚本中包含我的文件没有预期的效果。
/**
* Swap current users plan to a new one.
*
* @return 'Illuminate'Contracts'Routing'ResponseFactory|'Symfony'Component'HttpFoundation'Response
*/
public function swapPlan() {
if ($this->user->subscribed() && Input::get('plan')) {
$this->user->subscription(Input::get('plan'))->swap();
return response(trans('app.planSwapSuccess', ['plan' => Input::get('plan')]), 200);
$sale_amount = '10.00';
$product = 'Drive-Plan';
include('partners/controller/record-sale.php');
}
}
页面将完成加载而不会出错,但是不会执行应由包含的文件完成的功能。任何关于我可能做错了什么的想法,我们非常感谢。
我花了几天时间试图弄清楚这一点。由于我的测试文件有效,我猜我在完整脚本中缺少一些明显的东西。
谢谢你的关注。
完整文件:
<?php namespace App'Http'Controllers;
use App;
use Auth;
use Input;
use Stripe'Stripe;
use Stripe'Plan;
class PaymentsController extends Controller {
public function __construct() {
$this->middleware('loggedIn');
$this->middleware('paymentsEnabled');
$this->user = Auth::user();
$this->settings = App::make('App'Services'Settings');
Stripe::setApiKey($this->settings->get('stripe_secret_key'));
}
/**
* Subscribe user to a plan or swap him to a different plan.
*
* @return response
*/
public function upgrade() {
if ($this->user->subscribed()) {
$this->user->subscription(Input::get('plan'))->swap();
} else {
$this->user->subscription(Input::get('plan'))->create(Input::get('stripe_token'), ['email' => $this->user->email]);
}
return response(trans('app.upgradeSuccess'), 200);
$sale_amount = '10.00';
$product = 'Drive-Plan';
include('partners/controller/record-sale.php');
}
/**
* Swap current users plan to a new one.
*
* @return 'Illuminate'Contracts'Routing'ResponseFactory|'Symfony'Component'HttpFoundation'Response
*/
public function swapPlan() {
if ($this->user->subscribed() && Input::get('plan')) {
$this->user->subscription(Input::get('plan'))->swap();
return response(trans('app.planSwapSuccess', ['plan' => Input::get('plan')]), 200);
$sale_amount = '10.00';
$product = 'Drive-Plan';
include('partners/controller/record-sale.php');
}
}
/**
* Attach new credit card to user.
*
* @return 'Illuminate'Contracts'Routing'ResponseFactory|'Symfony'Component'HttpFoundation'Response
*/
public function addNewCard() {
$this->user->updateCard(Input::get('stripe_token'));
return response(trans('app.cardAddSuccess'), 200);
}
/**
* Resume a canceled subscription.
*/
public function resumeSubscription() {
$this->user->subscription(Input::get('plan'))->resume(Input::get('token'));
return $this->user;
$sale_amount = '10.00';
$product = 'Drive-Plan';
include('partners/controller/record-sale.php');
}
/**
* Cancel users subscription.
*
* @return 'App'User
*/
public function unsubscribe() {
$this->user->subscription()->cancel();
return $this->user;
}
/**
* Return current users invoices.
*
* @return array
*/
public function getInvoices() {
return view('invoices')->with('invoices', $this->user->invoices())->with('settings', $this->settings);
}
/**
* Download invoice with given id.
*
* @param {int|string} $id
* @return 'Symfony'Component'HttpFoundation'Response
*/
public function downloadInvoice($id) {
return $this->user->downloadInvoice($id, [
'vendor' => $this->settings->get('invoiceVendor'),
'product' => $this->settings->get('invoiceProduct'),
]);
}
/**
* Return all created plans.
*
* @return array
*/
public function getPlans() {
$plans = Plan::all();
$formatted = [];
foreach($plans->data as $plan) {
$formatted[] = [
'interval' => $plan['interval'],
'name' => $plan['name'],
'amount' => $plan['amount'] / 100,
'currency' => $plan['currency'],
'id' => $plan['id'],
'created' => $plan['created'],
];
}
usort($formatted, function($a1, $a2) {
if ($a1['created'] == $a2['created']) return 0;
return ($a1['created'] < $a2['created']) ? -1 : 1;
});
return $formatted;
}
}
您的方法在返回时结束,这意味着您永远不会到达正在设置的方法和变量中的包含。
举个例子:
将所述包含和变量移动到返回之前应该可以解决问题。
/**
* Resume a canceled subscription.
*/
public function resumeSubscription() {
$this->user->subscription(Input::get('plan'))->resume(Input::get('token'));
// Moved to before return
$sale_amount = '10.00';
$product = 'Drive-Plan';
include('partners/controller/record-sale.php');
return $this->user;
// UNREACHABLE
//$sale_amount = '10.00';
//$product = 'Drive-Plan';
//include('partners/controller/record-sale.php');
}