包含的文件不受尊重

included files not respected

尝试在脚本中包含 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;
    }
}

您的方法在 return 结束,这意味着您在方法中包含的内容和正在设置的变量永远不会到达。

一个例子:

将所述包含和变量移动到 return 之前应该可以解决问题。

 /**
 * 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');
}