【问题标题】:How to auto import all Stripe classes into CakePHP如何将所有 Stripe 类自动导入 CakePHP
【发布时间】:2015-02-20 15:55:57
【问题描述】:

如何将所有必需的类从 Stripe PHP SDK (2.1.0) 导入 CakePHP (2.6.1)?

我为 Stripe 添加了一个子模块并将其克隆到 app/Vendor/Stripe

我的代码如下所示:

<?php

App::uses('Stripe', 'Vendor/Stripe/lib');

// Secret API Key for Stripe
\Stripe\Stripe::setApiKey('sk_test_xxxxxxxxxxxxxxxxxxx');


class BillingController extends AppController {

// My methods

}


?>

我得到:

Fatal Error
Error: Class 'Stripe\Stripe' not found  
File: /Projects/Web/admin.example.io/app/Controller/BillingController.php   
Line: 6

【问题讨论】:

  • 您的 Stripe 文件夹在哪里?
  • app/Vendor/Stripe/lib/Stripe.php
  • 停止尝试重新发明轮子,使用作曲家。
  • 好吧,如果我使用composer,如何导入require_once('vendor/autoload.php');

标签: php cakephp cakephp-2.0 stripe-payments


【解决方案1】:

您应该在使用 vendor 时使用 import(),而不是 uses() 喜欢

App::import('Vendor', 'Stripe', array('file' => 'Stripe' . DS .'lib' . DS . 'Stripe.php'));

这里的 DS 是 / 的常量。

在使用 Vendor 包时,您应该遵循像 import() Must 这样的 cakephp 命名约定。在使用其他类(例如控制器)时,您可以使用 uses() 但在您的情况下不能使用。

更新:

现在步骤,您需要遵循,因为它更针对您的问题:

1.使用作曲家安装。

2.现在使用

导入已安装的包/供应商

App::import('Vendor', 'autoload', array('file' =&gt; 'Stripe' . DS .'vendor' . DS . 'autoload.php'));

【讨论】:

  • 这适用于设置密钥的初始方法,所以非常感谢,几乎就在那里,但如果我尝试通过 Stripe 文档中宣传的第二种方法检索客户列表(\Stripe \Customer::all()),我得到 Fatal Error Error: Class 'Stripe\Customer' not found ...问题真的是我如何导入所有必要的类,你能帮忙吗?
  • 如果我随后导入客户,它会因错误而失败:找不到类'Stripe\ApiResource'。不幸的是,无论出于何种原因,我无法以相同的方式导入? :(
  • 你加载了客户类吗?当您调用静态方法 all() 时。
  • 是的,我做到了.... App::import('Vendor', 'Stripe', array('file' => 'Stripe' . DS .'lib' . DS . 'Stripe .php')); App::import('Vendor', 'Customer', array('file' => 'Stripe' . DS .'lib' . DS . 'Customer.php')); App::import('Vendor', 'ApiResource', array('file' => 'Stripe' . DS .'lib' . DS . 'ApiResource.php'));
  • 你读完整了吗? github.com/stripe/stripe-php 它告诉了如何使用它。
【解决方案2】:

您应该包含 init.php 文件。

压缩文件的目录结构是这样的:

stripe-php - libs/ - data/ - test/ - init.php

不要将 PHP 文件包含在 libs 文件夹中,只需包含 init.php。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2017-11-04
    • 2018-12-26
    • 2015-09-10
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    相关资源
    最近更新 更多