0

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

1 回答 1

0

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

一个例子:

将所说的包含和变量移到返回之前应该可以解决问题。

 /**
 * 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');
}
于 2016-02-14T21:31:21.757 回答